Set x-axis tick labels in subplots

조회 수: 147 (최근 30일)
kyle mani
kyle mani 2022년 5월 25일
댓글: dpb 2022년 5월 26일
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...

답변 (2개)

Jan
Jan 2022년 5월 25일
편집: Jan 2022년 5월 25일
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
kyle mani
kyle mani 2022년 5월 25일
편집: kyle mani 2022년 5월 25일
Hi, thank you for your answer and fast response, I appreciate it! However, I am still confused on how to fix the x-axis labels... In the image, it can be seen clearly that the x-labels and x-ticks in the 4th plot are vastly different than in plots 1-3.
That is my primary concern. Any advice would be appreciated.
Jan
Jan 2022년 5월 26일
@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
dpb 2022년 5월 25일
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?
  댓글 수: 3
dpb
dpb 2022년 5월 26일
편집: dpb 2022년 5월 26일
I can't duplicate the symptom of your figure that you seem to have removed -- post a working example (including the data to run it) that reproduces the case.
As noted above, for date axes, unless you're using an old release of MATLAB (prior to about R2014b or so I believe) I'd strongly recommend using datetime for the time variable instead of datenum. plot then is self-aware and can get rid of the old, klunky datetick
Which ML release are you using?
dpb
dpb 2022년 5월 26일
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

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by