Borrar filtros
Borrar filtros

How to start my plot from the beginning of the x-axis?

16 visualizaciones (últimos 30 días)
gsourop
gsourop el 12 de Mayo de 2017
Editada: gsourop el 12 de Mayo de 2017
Hi everyone,
I try to create a plot where I want to assign in the x-axis the dates, starting from 01:1974 to 12:2014. If I create the equivalent number of random variables, ie 492, and plot it, I get some emplty space at the beginning of the x-axis. The code is the following
time = datetime(1974,1:492,1);
y1 = randn(492,6);
y2 = randn(492,6);
figure;
k = 1 : 6;
a= 0 :5;
varnames_y1 ={'A ', 'B', 'C' , 'D' , 'E', 'F'}
for i = 1:6;
subplot(6,2,a(i)+ k(i))
filename = plot( time' , y1( : , i ) );
title ( varnames_y1(i));
hold on
end
varnames_y2 ={'G ', 'H', 'I' , 'J' , 'K', 'L'}
for i = 1:6;
subplot(6,2,a(i)+ k(i) + 1)
filename = plot( time' , y2( : , i ) );
title ( varnames_y2(i));
hold on
end
If you run it you will see that the dates instead of starting from 01:1974, start from 1969. Hence, I get empty space until 01:1974. I would appreciae any help. Thanks in advance.

Respuesta aceptada

KSSV
KSSV el 12 de Mayo de 2017
clc; clear all ;
time = datetime(1974,1:492,1);
y1 = randn(492,6);
y2 = randn(492,6);
figure;
k = 1 : 6;
a= 0 :5;
varnames_y1 ={'A ', 'B', 'C' , 'D' , 'E', 'F'} ;
for i = 1:6;
subplot(6,2,a(i)+ k(i))
% filename = plot( time' , y1( : , i ) );
x = datenum(time) ;
filename = plot( x' , y1( : , i ) );
axis tight
datetick('x','mm:yyyy','keeplimits')
title ( varnames_y1(i));
hold on
end
varnames_y2 ={'G ', 'H', 'I' , 'J' , 'K', 'L'} ;
for i = 1:6;
subplot(6,2,a(i)+ k(i) + 1)
x = datenum(time) ;
filename = plot( x' , y2( : , i ) );
axis tight
datetick('x','mm:yyyy','keeplimits')
title ( varnames_y2(i));
hold on
end
  1 comentario
gsourop
gsourop el 12 de Mayo de 2017
Editada: gsourop el 12 de Mayo de 2017
It works fine. However, the only problem is that the ticks are very rare. Even if I change the line
datetick('x','mm:yyyy','keeplimits')
to
datetick('x','yyyy','keeplimits')
From 1974 to 2014 I can only see 4 ticks (1980, 1990,200,2010) making impossible to read the graph for dates in between these ticks. Could you help me change that?

Iniciar sesión para comentar.

Más respuestas (1)

Rik
Rik el 12 de Mayo de 2017
You can use the xlim function (or on older releases the axis function) to do this. But as xlim only accepts numerical input, you need to convert the time to a number. Hence the following line of code pasted after the plot command should do the trick.
xlim(datenum([min(time) max(time)]))

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by