The following simplified example illustrates my problem:
Let
DateStrings = {'2014-01';'2014-02';'2014-03';'2014-04';'2014-05';'2014-06'};
X = datetime(DateStrings,'InputFormat','y-M');
Y = 1:6;
The following works, but is not exactly what I need:
plot(X,Y, 'DatetimeTickFormat','M-yyyy');
What I need is something like this:
area(X,Y, 'DatetimeTickFormat','M-yyyy');
This does not work, because "Input arguments must be numeric".
Does anybody know how to put dates on the x-axis with "area" or fill the area between 0 and Y with plot?

 채택된 답변

Image Analyst
Image Analyst 2016년 2월 29일

0 개 추천

Don't use X. Just create your cell array DateStrings the way you want it and then do this:
area(1:length(DateStrings),Y);
ax = gca;
ax.XTickLabel = DateStrings;
ax.XTickLabelRotation = -75;

댓글 수: 3

Frank
Frank 2016년 2월 29일
Thank you very much for your answer. When I follow your code, I see the following X-Y-combinations. (2014-01, 1), (2014-02, 1.5), (2014-03, 2), (2014-04, 2.5), (2014-05, 3), (2014-06, 3.5), (2014-01, 4), ..., (2014-05, 6). This is not correct.
Image Analyst
Image Analyst 2016년 2월 29일
What's not correct about it? Like I said "Just create your cell array DateStrings the way you want it" <=== Did you do that? Or not?
Frank
Frank 2016년 3월 1일
Of course, I did that ....... not ;-). Thanks for your answers!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Peter Perkins
Peter Perkins 2016년 11월 7일

1 개 추천

Frank, if you have access to the most recent release, R2016b, area does now work with datetimes:
area(X,Y);
h = gca; h.XAxis.TickLabelFormat = 'M-yyyy'
The default labels are month name, with year as the "secondary label", so you'd need to set XAxis.TickLabelFormat to get exactly the labels you want. Notice that using XAxis.TickLabelFormat is a bit different than in previous releases. That new property is supported for all datetime plots, and most (maybe even all, I forget) plotting now works with datetimes and durations. DatetimeTickFormat is still supported in the plot function, but moving forward you should use XAxis.TickLabelFormat.
Hope this helps.
Einar
Einar 2016년 11월 3일

0 개 추천

An alternative solution:
area(datenum(X),Y)
datetick('x', 'mm-yyyy')
Frank
Frank 2016년 11월 3일

0 개 추천

So simple! Works perfect! Thank you very much.

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

질문:

2016년 2월 29일

답변:

2016년 11월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by