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?
0 个评论
采纳的回答
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 个评论
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
2022-8-10
Scientific notation is always disabled when you supply your own tick labels. Scientific notation is only for automatic labeling.
2 个评论
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 Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!