X axis tick labels

24 views (last 30 days)
Lejla Latifovic
Lejla Latifovic on 29 Sep 2022
Edited: dpb on 29 Sep 2022
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!

Accepted Answer

dpb
dpb on 29 Sep 2022
Edited: dpb on 29 Sep 2022
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.
  2 Comments
dpb
dpb on 29 Sep 2022
Edited: dpb on 29 Sep 2022
I know... :)

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 29 Sep 2022
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;

Tags

Community Treasure Hunt

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

Start Hunting!