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

24 次查看(过去 30 天)
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.

采纳的回答

KSSV
KSSV 2017-5-12
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 个评论
gsourop
gsourop 2017-5-12
编辑:gsourop 2017-5-12
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?

请先登录,再进行评论。

更多回答(1 个)

Rik
Rik 2017-5-12
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)]))

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by