Independent XTickLabel and YTickLabel font sizes

36 次查看(过去 30 天)
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