Problem with 'keeplimits' for dates on figure x-axis

2 ビュー (過去 30 日間)
Luc
Luc 2015 年 8 月 4 日
コメント済み: Brendan Hamm 2015 年 8 月 6 日
Hi,
I've search for an answer to my question but I still cannot get this to work. I am trying to plot data on a figure with the date on the x-axis. My problem is that the figure extends beyond the x-axis limits, even if I am using 'keeplimits'. The figure keeps showing an x-axis extending from May 11 to Oct 24 instead of the May 20 to Oct 22
t = datetime(2009,05,19) + caldays(1:156);
Ya = plot (t, wtddata(:,1),'-','LineWidth',2);
hold on; Yb = plot (t, wtddata(:,2),'-','LineWidth',2); hold off;
datetick('x','mmm','keeplimits')
Any help would be appreciated.
Thanks

採用された回答

Campion Loong
Campion Loong 2015 年 8 月 5 日
編集済み: Campion Loong 2015 年 8 月 5 日
Hi Luc,
I see that you are utilizing the datetime datatype introduced in R2014b. MATLAB picks an optimal set of ticks and labels when you plot a datetime array.
In case - such as yours - when MATLAB's optimal set of ticks is not the most suitable, you may pan and/or zoom the plot to get your desired layout. MATLAB will automatically update the ticks and labels to match the new limits after panning/zooming.
Most importantly, only use datetick when you are plotting dates and times represented in numeric values that are serial date numbers, rather than datetime arrays.
Campion
  2 件のコメント
Luc
Luc 2015 年 8 月 6 日
Thanks for the precisions.
I got it to work serial date numbers. Now I am wondering how do I get Matlab to show tick labels only for the 1st day of the month? My problem is that now when I write:
datetick('x','mmm','keepticks','keeplimits')
the figure will show the same month twice on the x axis ex: May Jun Jul Jul Aug
Thanks
Brendan Hamm
Brendan Hamm 2015 年 8 月 6 日
After an answer has been accepted, it is best to ask a new question in a new thread to get a timely response. That being said there is a way to use datetimes and change the Format displayed when you call the plot function. However, to change the location of the ticks on the x-axis you need to use MATLAB serial date numbers:
d = datetime('01-Jan-2015') + days(0:10:50);
y = randn(size(d));
ax = axes;
plot(d,x,'DatetimeTickFormat','MMM');
numMonths = month(between(d(1),d(end)));
xTick = datetime('01-Jan-2015') + calmonths(0:numMonths)
xTick = datenum(xTick);
ax.XTick = xTick;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by