How to remove 'year' in the x-axis

54 ビュー (過去 30 日間)
Zhiyun
Zhiyun 2023 年 3 月 2 日
コメント済み: Zhiyun 2023 年 10 月 17 日
Hi, I used the following command to make a plot. I don't wan to show the "year", but I got this plot. The x format is "datetime".
plot(x,y,'DateTimeTickFormat','MM/dd HH:mm')
Unrecognized function or variable 'x'.

採用された回答

the cyclist
the cyclist 2023 年 3 月 2 日
The accepted answer to this question gives the following method:
XDates = [datetime(2021,6,1:30) datetime(2021,7,1:31)];
YNumsForXDates = sin(0:0.1:6);
% Figure with year
figure
plot(XDates,YNumsForXDates)
% Figure without year
figure
plot(XDates,YNumsForXDates)
ax = gca;
ax.XTickLabel = ax.XTickLabel;
I have to admit that I don't fully under the mechanism of how this works. I wouldn't have expected the line
ax.XTickLabel = ax.XTickLabel;
to do anything at all! I believe it has something to do with the fact that it changes the XTickLabelMode to 'manual'.
  2 件のコメント
Zhiyun
Zhiyun 2023 年 3 月 3 日
This is amazing! I tried it and yes it works. Thank you.
Benjamin Kraus
Benjamin Kraus 2023 年 3 月 13 日
The year is being displayed in what my team calls the "secondary label". The string displayed in the secondary label is calculated as part of the automatic tick label generation. At the moment, there is no way to directly modify (or hide) the secondary label, but stay tuned. We have heard your requests, and are looking into options to allow for customization of the secondary label.
As for why your code hides the year. As @the cyclist guessed, this line:
ax.XTickLabel = ax.XTickLabel;
Is (for the most part) equivalent to:
ax.XTickLabelMode = 'manual';
Once you have set the tick labels (which toggles the mode to 'manual'), the ruler is no longer in control over what is displayed in the tick labels, and therefore has no way to know what should and should not be displayed in the secondary label, so it hides it.

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

その他の回答 (1 件)

Abby Skofield
Abby Skofield 2023 年 9 月 20 日
移動済み: Adam Danz 2023 年 10 月 17 日
Starting in MATLAB R2023b, you can add, remove, or update the secondary labels using the new functions xsecondarylabel, ysecondarylabel, zsecondarylabel.
t=datetime(2019,1,1,0,0:15:365*24*60-15,0);
plot(t,cumsum(randn(size(t))))
title('2019')
xsecondarylabel(Visible='off')
  1 件のコメント
Zhiyun
Zhiyun 2023 年 10 月 17 日
Great! Thank you!

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

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by