# Set x-axis tick labels in subplots

37 views (last 30 days)
kyle mani on 25 May 2022
Commented: dpb on 26 May 2022
Hi all,
I am subplotting 10 different figures, which plot yearly values of a specific variable. For 8 out of the 10 plots, I get nicely spaced x-axis labels. For 2, I get just 4 labels and I cannot figure out why there is a discrepancy. The code for all 10 subplots is all the same. Can someone tell me why I am getting a discrepancy? I have tried to manually set the x-labels as dates, but cannot seem to get MATLAB to work.
Here is my sample code for a single subplot.
subplot(5,2,4)
plot(DateFour,(Four+Pedsfour)) %DateFour is a datetime vector and the y variable is a vector of numbers.
tx = get(gca,'XTickLabel'); set(gca,'XTickLabel',tx,'fontsize',10,'FontWeight','bold');
ty = get(gca,'XTickLabel'); set(gca,'XTickLabel',ty,'fontsize',10,'FontWeight','bold');
datetick('x','dd mmm yy','keeplimits','keepticks');
hAx=gca;
set(hAx,'YGrid','on','XGrid','on')
set(hAx,'xminorgrid','on','yminorgrid','on')
Here is a picture in which you can see the 4th plots x-ticks and labels are different than the previous 3 plots...

Jan on 25 May 2022
Edited: Jan on 25 May 2022
Avoid gca if you can get the axes handle directly:
axesH = subplot(5,2,4);
plot(axesH, DateFour, Four+Pedsfour);
Why do you obtain the XTickLabels an set them directly again?
tx = get(gca,'XTickLabel');
set(gca,'XTickLabel',tx,'fontsize',10,'FontWeight','bold');
This should be enough:
set(axesH, 'fontsize', 10, 'FontWeight', 'bold');
And why do you do this twice:
tx = get(gca,'XTickLabel'); set(gca,'XTickLabel',tx,'fontsize',10,'FontWeight','bold');
ty = get(gca,'XTickLabel'); set(gca,'XTickLabel',ty,'fontsize',10,'FontWeight','bold');
?? Maybe the 2nd one should by 'YTickLabel'?
This would be a typical error of code created by exhaustive copy&pasting. Whenever you do this to create a bunch of similar code blocks, think twice. Drink a cup of tea or coffee and create a loop instead. Redundant code cries for such typos.
By the way, it might be easier to define the font size ones during creating the figure:
FigH = figure('DefaultAxesFontSize', 10, ...
'DefaultAxesFontWeight', 'bold');
##### 2 CommentsShowHide 1 older comment
Jan on 26 May 2022
@kyle mani: As I have written before, this might be caused by the code. If this is really the used code:
tx = get(gca,'XTickLabel'); set(gca,'XTickLabel',tx,'fontsize',10,'FontWeight','bold');
ty = get(gca,'XTickLabel'); set(gca,'XTickLabel',ty,'fontsize',10,'FontWeight','bold');
You set the XTicks twice and forgot the YTicks. I assume further typos in the code also. Therefore I suggested several methods to simplify the code massively, because then such errors can be found much easier.

dpb on 25 May 2022
There's nothing in that code to rotate the x axis labels that must be in the previous three.
As general comment, would be better to use datetime instead of datenum; plot then will automagically format the axis with time and you can change the format string to suit. It's much cleaner/easier than the deprecated datetick route.
Also, it would be better code construction to use arrays to hold the data instead of
subplot(5,2,4)
plot(DateFour,(Four+Pedsfour)
making new variables with long names to pick the sections that must have done it would appear.
Then you can put it all in a loop and have the same code in the loop and not have to duplicate the same stuff over and over and over for every plot.
N=5; M=2; % the number of subplots to create
for i=1:N*M
hAx(i)=subplot(N,M,i); % create axes, save handle
x=dataY(Y(i)); y=dataX(Y(i)); % however to index into the big data to get the given year
hL(i)=plot(x,y);
hAx(i).FontSize=10; hAx(i).FontWeight='bold';
datetick('x','dd mmm yy','keeplimits','keepticks');
grid('on'), grid('minor')
end
would ensure all will be alike.
One result of not writing generic code but duplicating code over and over is probably the cause of your specific problem here -- in
ty = get(gca,'XTickLabel'); set(gca,'XTickLabel',ty,'fontsize',10,'FontWeight','bold');
you duplicated the precding line it appears and didn't change 'X' to 'Y'.
But, this is an exceedingly verbose way to do what you're doing, anyway -- why do you use gca repeatedly and then retrieve handles and use set the same way when there is a direct straightforward way to address the property directly?
dpb on 26 May 2022
BTW, unless you set the tick values, ML will try to "prettify" the axes ticks to fit even intervals and spacing so you get the tick marks it picks -- if there is any rounding of the times in your time vector, that could possibly change the number of tick marks it picks.
The xticks for datenum are just datenum() values; you can compute those from datenum() to match what you want and write those, but I'd again suggest datetime instead and then you can set the tick values where you want them with actual dates without datetick

### Community Treasure Hunt

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

Start Hunting!