Independent XTickLabel and YTickLabel font sizes

27 views (last 30 days)
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?

Accepted Answer

Steven Lord
Steven Lord on 2 Aug 2016
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 Comments
John
John on 2 Aug 2016
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 on 2 Aug 2016
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');

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!