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!