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

69 ビュー (過去 30 日間)
haohaoxuexi1
haohaoxuexi1 2022 年 8 月 10 日
コメント済み: dpb 2022 年 8 月 10 日
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 Center および File ExchangeAxis Labels についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by