Is it not possible to change every font size on plots at the same time?
Show older comments
Is it not possible to change every font size on plots at the same time with only one command? (tics, legends, titles, axes, etc.)
Accepted Answer
More Answers (6)
Walter Roberson
on 6 Jun 2018
Edited: Walter Roberson
on 28 Jan 2020
To change all text objects to have a particular font size, then
fig = gcf; %or one particular figure whose handle you already know, or 0 to affect all figures
set( findall(fig, '-property', 'fontsize'), 'fontsize', NewFontSize)
Note that this might not find the font size information on some of the more obscure graphics objects where it might be hidden some levels down. For example I would not assume that this code would be able to locate the font size information of the individual labels written into heatmap objects, or the font size information for contour labels created with clabel()
12 Comments
Walter Roberson
on 28 Jan 2020
When I was writing my own layout code, I adopted the practice of adding a Tag (or perhaps it was UserData) to each object that I wanted to control the font size of, with the tag having text content similar to 'fs(-2)' or 'fs(+3)' . I then provided the user with a method to change the default font size. I could then search for all of those tags and use the number as relative font size compared to the base font size the user chose. This gave a method for the relative font sizes to be preserved for the purpose of information output (e.g., headers vs detail) while still permitting the user to adjust the basic font size to suit themselves.
If I recall correctly, my code also re-flowed text and even adjusted the size and position of gui objects as appropriate to hold the text with the changed font size, so if the user requested a large font then the text presentation of the controls would not be cut off.
dpb
on 29 Jan 2020
You clearly had too much time on your hands, Walter... :)
Adam Danz
on 29 Jan 2020
The relative font sizes can be preserved by scaling them like this. That's more or less what my supersizeme() function does.
% Demo figure
plot(rand(4,5))
xlabel('x axis')
ylabel('yaxis')
title('Large Title','FontSize',16)
text(2,2,'Some Axis Text','FontSize', 12)
legend('A','B','C','D','E','FontSize',6)
% Increase font sizes by a factor of 1.2
h = findall(gcf, '-property', 'fontsize')
hFontSize = cell2mat(get(h,'FontSize'));
newFontSize = hFontSize * 1.2;
set(h,{'FontSize'}, num2cell(newFontSize))
Walter Roberson
on 29 Jan 2020
My boss used an ugly lower-resolution monitor with odd aspect ratio, so my GUI had to be adaptive to the available screen real-estate ;-)
Jan Grüner
on 29 May 2020
Sadly this doesn't seem to work when using tiledlayout(). I'm using it combined with two heatmaps and findall() will only return the properties for the heatmaps.
Can somone confirm this?
Adam Danz
on 29 May 2020
@Jan Grüner , I just tested it with r2020a using the code below. The supersizeme() I mention in my answer works for tiled layout and it uses the findall(gcf, '-property', 'fontsize') line to get the text handles. If you could attach your figure and let us know what release of matlab you're using, we might get better info.
tiledlayout(1,2)
nexttile
heatmap(rand(3,3))
xlabel('x')
ylabel('y')
title('t1')
nexttile
heatmap(rand(3,3))
xlabel('x')
ylabel('y')
title('t2')
Jan Grüner
on 29 May 2020
Edited: Jan Grüner
on 29 May 2020
By my logic the following should change the font settings on all elements. However the "main labels" are not changed. Or am I missing something here?
hf = figure();
ht = tiledlayout(2,1);
title(ht, 'mainTitle');
xlabel(ht, '[xLabel]');
ylabel(ht, '[yLabel]');
nexttile();
heatmap(randi(10,4), 'Colormap', hot);
title('upper');
nexttile();
heatmap(randi(10,4), 'Colormap', hot);
title('lower');
set(findall(hf, '-property', 'FontSize'), 'FontSize', 8);
set(findall(hf, '-property', 'FontName'), 'FontName', 'Arial');
Using R2020a Update2
edit: switched order of heatmap() & title() to display "subtitles" as well. Problem is still the not changing font settings for mainTitle, [xLabel] and [yLabel]
Adam Danz
on 29 May 2020
I see what you mean now. It works with my example because the axis lables are children of each axis but in your example, the axis labels and title are properites of the tiled chary layout. I don't have time to look deeper into this now but I might come back to it at some point.
Walter Roberson
on 29 May 2020
It appears that HeatmapChart objects have a hidden property TitleHandle that has the text() object, and it appears that findall() is not finding the hidden properties of HeatmapChart objects.
Adam Danz
on 30 May 2020
In the example Jan Grüner gave 3 comments up, the fontsize change does affect the heatmap labels and titles. It doesn't affect the tiledlayout labels and title. Those are the objects that have hidden handles that findall() isn't accessing (I'm using r2020a). However, if you the fontsize is increased to something like 20, the heatmap label fontsize has a ceiling effect so that the labels never exceed the cell size
Junho Kweon
on 14 Mar 2021
@Walter Roberson Thank you for your sharing! It works greatly. By the way, when I want to set other elements with using your fontsize setting, how can I do it?
In other words, can I use these two lines into one 'set' function form?
set(gca,'FontWeight','bold','LineWidth',2)
set(findall(gcf,'-property','FontSize'),'FontSize',14)
Thanks,
Walter Roberson
on 14 Mar 2021
set(findall(gcf,'-property','FontSize'),'FontSize',14, 'fontweight', 'bold', 'LineWidth', 2)
However it would be better to record the handles and specifically test them for having the other properties, in case there are objects that allow you to control some but not all of the properties.
Tom Baldock
on 12 Feb 2020
Combining a few threads which did not quite work, this seemed to work for me in R2019, placed at the end of a script, which modified all font size (axis names and numerical values, legens) on all open figures. You can then rerun these lines to get new sizes of any figure
fh = findall(0,'Type','Figure');
set( findall(fh, '-property', 'fontsize'), 'fontsize', 14)
Bruce Rodenborn
on 28 Jan 2020
This command, however, does change everything, at least in R2019a
a = get(gca,'XTickLabel');
set(gca,'XTickLabel',a,'fontsize',24)
though presumably this should only change the xticks.
MATLAB should make this all make sense - OY!
4 Comments
Walter Roberson
on 28 Jan 2020
Setting the XTickLabel back to what it already is, is redundant. You can
a = get(gca, 'XAxis');
set(a, 'FontSize', 24)
to change the fontsize for the X Axis only.
dpb
on 28 Jan 2020
a = get(gca,'XTickLabel');
set(gca,'XTickLabel',a,'fontsize',24)
"...though presumably this should only change the xticks."
NO! That sets the XTickLabel property to the content of a and then the 'FontSize' property to 24.
It's the same code as if you had written
set(gca,'XTickLabel',a)
set(gca,'fontsize',24)
the named argument pairs are totally independent of each other; the fact you set the content of the label and the font size in the same call is totally immaterial.
If I do:
figure(1)
title('Blah'); ylabel('Y'); xlabel('X')
set(gca,'FontSize',8)
a = get(gca,'XTickLabel');
set(gca,'XTickLabel',a,'fontsize',30)
It will make ALL the font on the figure to 30 point. Even though I set the fontsize to 8 earlier, the 'XTickLabel' seems to set the fonts for the whole figure even though, "presumably this should only change the xticks" as Bruce said.
This is really annoying because I am trying to change the font size of only the XTick labels.
Using MATLAB 2021a.
Walter Roberson
on 24 Mar 2022
set(gca,'XTickLabel',a,'fontsize',30)
You should not understand this as setting the fontsize for only the XTickLabel. You should understand this command as the combination of commands
set(gca,'XTickLabel',a)
set(gca,'fontsize',30)
Options in a single set() call are independent of each other (though in a few cases, order can matter.)
What you should be doing instead is
set(a, 'fontsize', 30)
dpb
on 29 May 2015
0 votes
Nope; they're individual properties of the various objects. You could do some building of some tools and/or link various properties together but there's no builtin function that does such globally, no.
You can, however, change the default properties for many things if that would help, to have plots be created more like you'd like initially.
Bruce Rodenborn
on 28 Jan 2020
0 votes
None of these work. The command does not change labels or tickmarks or legends unless I am missing something. A MWE would be helpful.
2 Comments
Walter Roberson
on 28 Jan 2020
I fixed a typing mistake in my Answer.
Adam Danz
on 28 Jan 2020
The function I recommended does work, even if you've used TeX markup (but not if you've used Latex).
Bruce Rodenborn
on 29 Jan 2020
0 votes
Sorry for being a bit sloppy and thanks for the corrections. I got the previous command I posted from another thread, which was addressing only changing tick mark labels, but it changed all fonts (xlabel, ylabel, legend and tick marks to 24. Thus, I inadvertently found some lines that did what I want, but did not fully understand them. It seems that all I ever needed was the command:
set(gca,'fontsize',24),
which does set the defaul font size for the current axes. If issued before I label, I can change other text elements without affecting the default, e.g.:
set(gca,'fontsize',16),
ylabel('y')
xlabel('x')
legend('series 1','series 2', 'series 3','series 4','FontSize',24)
changes the font sizes of everything, but the legend is font size 24, which is as much control as I hoped.
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
