Set font for tiled layout (axis tick label font)

I have a series of plots that are in a 2x1 tiled layout. I would like to change the font for all components of the plot to Helvetica, and I am especially keen to figure out how to do this for the axis tick labels.
Here is an example of my code:
figure(1)
t = tiledlayout(2,1);
ax1 = nexttile;
plot(time, avg_sst, 'color', 'r', 'LineWidth', 2)
ax1.XGrid = 'on';
ax1.XAxis.FontSize = 18;
ax1.YAxis.FontSize = 18;
ylabel('\circ C')
ax2 = nexttile;
plot(time, avg_sst_anom, 'color', 'k', 'LineWidth', 2)
ax2.XGrid = 'on';
ax2.XAxis.FontSize = 18;
ax2.YAxis.FontSize = 18;
ylabel('\circ C anom')
I have tried the following, but either end up with no change to the font, or an error message:
ax1.FontName = 'Helvetica';
ax1.XAxis.FontName = 'Helvetica';
ax1.XLabel.FontName = 'Helvetica';
t.FontName = 'Helvetica';
set(gca, 'FontName', 'Helvetica')
set(gca, 'fontname', 'Helvetica')
Thanks in advance :)

6 Comments

At least here, the default font is 'Helvetica' so won't see any visible change.
What does
ax1.XAxis.FontName
return?
BTW,
ax1.Fontname='Helvetica';
will change both X- and Y-axes fonts at same time; only need to address the individual axes objects if want them to be different from each other.
Try something noticeably different and then change back to prove is actually changing.
Change all of them at once using,
set([ax1, ax2], 'FontName', 'Helvetica')
or
set(get([ax1, ax2],'XAxis'), 'FontName', 'Helvetica')
But like dpb mentioned, that's the default font, at least on my end. To see your default axis font,
get(0,'defaultAxesFontName')
dpb
dpb on 26 Jan 2021
Edited: dpb on 26 Jan 2021
+1 to think of/suggest looking at system default setting...
You all are correct that the default is Helvetica -- thanks! The responses are still helpful, as I am going to change the front from Helvetica to something else.
All of the following work:
ax1.XAxis.FontName
set([ax1, ax2], 'FontName', 'Helvetica')
set(get([ax1, ax2],'XAxis'), 'FontName', 'Helvetica')
Weirdly, this one continues to throw an error, so I am changing the font on each axis individually:
ax1.Fontname='Helvetica';
The error tells you the problem. "Fontname" isn't a property.
But FontName is; exactly how you're using it in the previous lines in set().
>> figure
>> hAx=axes;
>> hAx.FontName='Arial';
>> set(hAx,'fontname','Helvetica')
>> get(hAx,'fontname')
ans =
'Arial'
>> hAx.Fontname
Unrecognized method, property, or field 'Fontname' for class 'matlab.graphics.axis.Axes'.
Did you mean:
>> hAx.FontName
ans =
'Helvetica'
>>
One of the quirks (and an irritating one at that) of MATLAB: get, set are case-insensitive but the "dot" notation isn't.

Sign in to comment.

Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

on 25 Jan 2021

Edited:

dpb
on 26 Jan 2021

Community Treasure Hunt

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

Start Hunting!