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

53 次查看(过去 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