X axis tick labels

조회 수: 165 (최근 30일)
Lejla Latifovic
Lejla Latifovic 2022년 9월 29일
편집: Adam Danz 2023년 10월 18일
Hello!
I'm looking for a little help in setting my x-axis labels. I'm sure I have a couple of different codes mixed in here and cannot figure out how to untangle the mess. I am trying to label each tick with the month of year and not have Jan show at the end. Or the 2021 label.
I keep getting this error when I try to set the xTicks and label them:
Unrecognized method, property, or field 'xtick' for class 'matlab.graphics.axis.Axes'.
Error in tpd_analysis_2012_2021 (line 1057)
ax.xtick([1 2 3 4 5 6 7 8 9 10 11 12]);
This is my code, xtick lines are at the bottom:
NEP_sync = [Annual_NEP_12;Annual_NEP_13;Annual_NEP_14;Annual_NEP_15;Annual_NEP_16;Annual_NEP_17;Annual_NEP_18;Annual_NEP_19;Annual_NEP_20;Annual_NEP_21];
years = unique(NEP_sync.Date.Year,'sorted');
fig = figure;
figure(1)
hold on
for i = 1:length(years)
NEPcum_year = NEP_sync(NEP_sync.Date.Year == years(i),:);
NEPcum_year.Date.Year = 2021; % set all datetime x-values to the same arbitrary year
plot(NEPcum_year.Date, NEPcum_year.CumulativeSum,'LineWidth',1);
end
xtickformat('MMM');
legend(string(years));
legend('Location','northwest','NumColumns',1)
legend boxoff
hold off
newcolors = {'#a9a9a9','#66cdaa','#dcbeff','#9A6324','#ff0000','#000000','#f58231','#ffd700','#00ff00','#006400','#42d4f4'};
colororder(newcolors)
xlabel('Month', 'fontweight','bold');
ylabel('Cumulative NEP (g C m^-^2)','fontweight','bold');
box on
ax = gca;
ax.YGrid = 'on';
ax.xtick([1 2 3 4 5 6 7 8 9 10 11 12]);
ax.xticklabels({'|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|'});
fig.Units = 'centimeters';
fig.Position(3) = 8;
fig.Position(4) = 6;
Any help would be much appreciated.
Thank you!

채택된 답변

dpb
dpb 2022년 9월 29일
편집: dpb 2022년 9월 29일
The x-axis is not numeric but a @doc:datetime
Hmm...I thought they had made that a visible property (bells ringing, @Adam Danz <VBG>) but I don't see it (thru R2020b) -- try this
...
xtk=xticks; % retrieve the tick locations (a datetime array)
xticks(xtk(1:end-1)) % set back all except last one
hAx=gca; % get the axis handle
hAx.XAxis.SecondaryLabel.Visible='off'; % hide the reference year display
The doc page for <DatetimeRuler Properties> still doesn't have a visible property to turn the secondary label on/off.
  댓글 수: 3
dpb
dpb 2022년 9월 29일
편집: dpb 2022년 9월 29일
I know... :)
Adam Danz
Adam Danz 2023년 10월 18일
편집: Adam Danz 2023년 10월 18일

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

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 9월 29일
The function for setting the X ticks on an axes is xticks not xtick. It is not a method of the axes object so you need to call it without passing the axes object into it or with the axes object inside the parentheses as an input argument.
ax = axes;
xticks(ax, (1:2:10)/10)
Alternately you could set the XTick property of the axes.
figure
ax2 = axes;
ax2.XTick = (0:2:10)./10;
  댓글 수: 1
Lejla Latifovic
Lejla Latifovic 2022년 9월 29일
Thank you!

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

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by