Azzera filtri
Azzera filtri

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

16 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

KSSV
KSSV il 12 Mag 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 Commento
gsourop
gsourop il 12 Mag 2017
Modificato: gsourop il 12 Mag 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?

Accedi per commentare.

Più risposte (1)

Rik
Rik il 12 Mag 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)]))

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by