Set date limits into x-axis

7 次查看(过去 30 天)
Hi everyone,
I have created several plots into a single figure, but the problem is that the x-axis (dates) are not consistent with my data, so there are gaps between the beginning of the a-axis and the graphs' line and the end of graphs' line and the end of the x-axis. I applied the following code:
% I want to create the x-axis, that is the dates (432x1), ie. starting from 1979:01 to 2014:12
DateNumber = datenum(1979,1:432,1);
formatOut='mmmm-dd-yyyy';
date = datestr(DateNumber,formatOut);
% then I want to print the information in every y(:,i) into a graph by including the dates I created before.
% If y is a matrix 432x27, from 1979:01 to 2014:12, I just want to create the graph with this information
y = randn(432,27);
figure()
xlength = 0.30;
ylength = 0.10;
xstart = [0.05 0.39 0.73];
ystart = [0.02 0.13 0.24 0.35 0.46 0.57 0.68 0.79 0.90];
hold on
for iy=1:9
for ix = 1:3
ind = ix + 3*(iy-1);
coordinates = [xstart(ix) ystart(9-iy+1) xlength ylength];
subplot('Position',coordinates);
plot(DateNumber,ind*y(:,ind))
drawnow
datetick('x','mmm-yy','keepticks')
end
end
I would appreciate any help.Thanks in advance!

采纳的回答

Peter Perkins
Peter Perkins 2017-2-9
MATLAB graphics underwent a major upgrade a few releases back, so it's hard to know exactly what your figure looks like without knowing what release you're on, or better yet seeing an example and a clear description of what you are trying to get. So I'm not sure what problem you are running into. As Adam says, I think maybe the root cause is that you have a bunch of small plots, with not enough room to properly label the time axis.
If you have access to R2016b, this makes a pretty nice plot of your data, see attached:
time = datetime(1979,1:432,1);
y = randn(432,27);
tt = array2timetable(y,'RowTimes',time)
ttplot(tt)
ttplot is on the File Exchange. If you're using something between R2014b and R2016a, you can still do that using a table (rather than a timetable), it's just not quite as immediate.

更多回答(1 个)

Adam
Adam 2017-2-9
doc xlim
What is wrong with just using this to set the limits?
  2 个评论
gsourop
gsourop 2017-2-9
All the examples are arithmetical in the x-axis and none is with dates.For example, I could use
DateNumber = datenum(1979,1:432,1);
formatOut='mmmm-dd-yyyy';
date = datestr(DateNumber,formatOut);
y = randn(432,27);
figure()
xlength = 0.30;
ylength = 0.10;
xstart = [0.05 0.39 0.73];
ystart = [0.02 0.13 0.24 0.35 0.46 0.57 0.68 0.79 0.90];
hold on
for iy=1:9
for ix = 1:3
ind = ix + 3*(iy-1);
coordinates = [xstart(ix) ystart(9-iy+1) xlength ylength];
subplot('Position',coordinates);
plot(DateNumber,ind*y(:,ind))
drawnow
datetick('x','mmm-yy','keepticks')
xlim([722816 735934])
end
end
but then x-axis give me only one point of time ''Sep-98'' and nothing else. So they split the sample into 2 parts and not 36 or 18 equal parts.
Adam
Adam 2017-2-9
Well, you can set 'XTick' for your axes to whatever you want if you want more ticks, but when I run your code the plots are so small there isn't room for many ticks. I see three when I do it, but ticks are a different issue to limits - they are controlled by a totally different property of the axes.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by