How to change the font size of xtick and ytick with scientific notation on the side?

조회 수: 40 (최근 30일)
xticklabel_fs = get(gca,'XTickLabel');
set(gca, 'XTickLabel', xticklabel_fs, 'FontName', 'Times New Roman', 'fontsize', 12);
yticklabel_fs = get(gca,'YTickLabel');
set(gca, 'YTickLabel', yticklabel_fs, 'FontName', 'Times New Roman', 'fontsize', 12);
Origin:
After applying the above code:
Can anyone tell me what to do with it?
After applying the above code, the font size is enlarged but the scientific notation disappear?

채택된 답변

dpb
dpb 2022년 8월 10일
x=linspace(0,2.1E-4); y=randn(size(x));
>> plot(x,y)
>> hAx=gca;
>> hAx.FontName='Times New Roman';
>> hAx.FontSize=12;
>> xlabel('X')
>>
seems to work just fine here...
I don't see anything specifically wrong in the posted code snippet but it's not necessary to reset the text to change the other properties.
You sure you didn't do anything else besides???
I have R2020b installed here, but I'd be very surprised if there were any warts in this area in R2018b; I've been updating usually a year or two behind since the beginning of time and have no recollection of the above not working as expected in any release.
  댓글 수: 2
haohaoxuexi1
haohaoxuexi1 2022년 8월 10일
ax = gca; ax_FS = 12;
ax.XAxis.FontSize = ax_FS; ax.XAxis.FontName = 'Times New Roman';
ax.YAxis.FontSize = ax_FS; ax.YAxis.FontName = 'Times New Roman';
I changed to this, and it works @dpb
dpb
dpb 2022년 8월 10일
Yes, that would, but it's twice the amount of code you need...there's no need to change X and Y axes properties individually; they inherit from the upper level. Only if you're going to set one different than the other is there any point in it.
>> hAx=gca;
>> hAx.FontName, hAx.XAxis.FontName,hAx.YAxis.FontName % show what is default
ans =
'Helvetica'
ans =
'Helvetica'
ans =
'Helvetica'
>> hAx.FontName='Times New Roman'; % change top level
>> hAx.FontName, hAx.XAxis.FontName,hAx.YAxis.FontName % show both X-, Y- inherit
ans =
'Times New Roman'
ans =
'Times New Roman'
ans =
'Times New Roman'
'Times New Roman'
>> hAx.XAxis.FontName='Helvetica'; % restore X-axis to default
>> hAx.FontName, hAx.XAxis.FontName,hAx.YAxis.FontName % Now it matters...
ans =
'Times New Roman'
ans =
'Helvetica'
ans =
'Times New Roman'
>>

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 8월 10일
Scientific notation is always disabled when you supply your own tick labels. Scientific notation is only for automatic labeling.
  댓글 수: 2
dpb
dpb 2022년 8월 10일
Good point, Walter. I'd not thought about that nuance in writing tick labels that disconnects them from the tick values also triggering the change in formatting; just knew it wasn't needed for what OP wanted.

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

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by