I am unable to change the fontsize of axis ticks

조회 수: 15 (최근 30일)
Chandramouli Santhanam
Chandramouli Santhanam 2023년 3월 10일
답변: VBBV 2025년 1월 18일
I am using MATLAB R2021b, and would like to change the font size of tick labels of both the axes. I tried the method suggessted here However, setting the font size using gca.FontSize doesn't change the size of axis ticks. Here's a sample code:
x = rand(1,100);
y = rand(1,100);
scatter(x,y)
ax = gca;
ax.FontSize = 20;
xlabel('My Label','FontSize',20)
I obtain a result as shown in the attachment. Only the xlabel size is changed to 20, and that is because of the last line I guess.
Can someone help me please?
Thank you.
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2023년 3월 10일
You code works as expected in R2022b. I don't have R2021b installed so can't check if it's a version-specific thing. However, I did check in R2020b and got the same result as below.
x = rand(1,100);
y = rand(1,100);
scatter(x,y)
ax = gca;
ax.FontSize = 20;
xlabel('My Label','FontSize',20)
Chandramouli Santhanam
Chandramouli Santhanam 2023년 3월 13일
I restarted MATLAB twice and now the code works. Seems strange

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

답변 (2개)

Adam Danz
Adam Danz 2023년 3월 10일
편집: Adam Danz 2023년 3월 10일
> setting the font size using gca.FontSize doesn't change the size of axis ticks
gca() is a function that returns the current axis handle. Unless you've used gca as a variable name, you should be getting an error (the error message below is from R2022b).
gca.FontSize
Dot indexing into the result of a function call requires parentheses after the function name. The supported syntax is 'gca().FontSize'.
% > Unable to resolve the name gca.FontSize. - R2021b error message
You could use
gca().FontSize
Or, better yet,
ax = gca();
ax.FontSize
  댓글 수: 1
Chandramouli Santhanam
Chandramouli Santhanam 2023년 3월 13일
Strange, I don't get this error with just gca. My problem got resolved by restarting MATLAB twice

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


VBBV
VBBV 2025년 1월 18일
@Chandramouli Santhanam, The below syntax should work better by using XAxis for axes handle
x = rand(1,100);
y = rand(1,100);
scatter(x,y)
ax = gca;
ax.XAxis.FontSize = 20; %
xlabel('My Label','FontSize',20)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by