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

22 views (last 30 days)
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.

Accepted Answer

KSSV
KSSV on 12 May 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 Comment
gsourop
gsourop on 12 May 2017
Edited: gsourop on 12 May 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?

Sign in to comment.

More Answers (1)

Rik
Rik on 12 May 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)]))

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!