Independent XTickLabel and YTickLabel font sizes

조회 수: 36 (최근 30일)
John
John 2016년 8월 2일
댓글: Steven Lord 2016년 8월 2일
Is it possible to set the font size of y-tick marks independently of the font size of the x-tick marks, ylabel and xlabel?
When I use:
set(gca,'YTick',[-pi 0 pi], 'YTickLabel', {'-\pi','0','\pi'}, 'fontsize', 18);
it sets the fonts size for all labels to the same size. Is there a standard MATLAB function to do this?

채택된 답변

Steven Lord
Steven Lord 2016년 8월 2일
The documentation includes examples that use the numeric rulers (introduced in release R2015b) that are part of the axes to do this type of customization. You can also change the properties of the objects stored in the XLabel and YLabel properties of the axes.
% Sample data
x = -1:0.01:1;
y = 3*asin(x);
% Plot it and retrieve the handles of the objects we're going to manipulate
h = plot(x, y);
yL = ylabel('Abracadabra');
xL = xlabel('For demonstration purposes only', 'Color', 'r');
ax = ancestor(h, 'axes');
yrule = ax.YAxis;
% Change properties of the axes
ax.YTick = [-pi 0 pi];
ax.YTickLabel = {'-\pi','0','\pi'};
% Change properties of the ruler
yrule.FontSize = 18;
% Change properties of the label
yL.FontSize = 8;
Some of the manipulation I did (in particular changing the YTick and YTickLabel properties of the axes) I could have done via several of the objects as well. But in order to change the font size of the X and Y axes independently I need the ruler. Changing the axes FontSize using ax would change all of the X tick labels, X label, Y tick labels, and Y label.
  댓글 수: 2
John
John 2016년 8월 2일
Thank you. That was what I was looking for. However, now I have another issue. If I try to plot 2 functions in the same figure I get the following error. "Struct contents reference from a non-struct array object."
For example:
% Sample data
x = -1:0.01:1;
y = 3*asin(x);
z = 3*acos(x);
% Plot it and retrieve the handles of the objects we're going to manipulate
h = plot(x, y, x, z);
yL = ylabel('Abracadabra');
xL = xlabel('For demonstration purposes only', 'Color', 'r');
ax = ancestor(h, 'axes');
yrule = ax.YAxis;
% Change properties of the axes
ax.YTick = [-pi 0 pi];
ax.YTickLabel = {'-\pi','0','\pi'};
% Change properties of the ruler
yrule.FontSize = 18;
% Change properties of the label
yL.FontSize = 8;
Any suggestions?
Steven Lord
Steven Lord 2016년 8월 2일
The plot function returns a two element vector of handles to lines. When you use that vector of handles as input to ancestor it returns a two element cell array where each element contains the axes ancestor of the corresponding line from the vector. Since they're in the same axes, just ask for the axes ancestor of one of the lines.
ax = ancestor(h(1), 'axes');

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by