1,580 views (last 30 days)
Show older comments
Morten Nissov on 20 Nov 2019
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels
Commented: Adam Danz on 14 Jun 2022
Accepted Answer: Adam Danz
Open in MATLAB Online
I often need to make pretty cumbersome plotting definitions in MATLAB, an example of which can be seen below
figure(1)
clf
subplot(221)
hold on
plot(z(1,:),...
'LineWidth',2)
line([0,N], [R(1,1),R(1,1)],...
'color','red','linestyle','--','LineWidth',2)
hold off
grid on
xlabel('$k$',...
'interpreter','latex','fontsize',14)
ylabel('$h_1$',...
'Interpreter','latex','FontSize',14)
legend({'closed loop','setpoint'},...
'location','best','fontsize',14)
For simplicities sake I have only included one of four subplots. In these plots I end up writing
'interpreter','latex'
and
'fontsize',14
quite a lot. I am asking if there is a better way to do this given that the font size and interpreter type is the same for my xlabel, ylabel, and legend which it very often is for me.
I have seen some pages recommending I use something along the lines of
set(gca,'fontsize',14)
But this doesnt work for the labels for me.
TLDR: What is a "good" coding style way of configuring the tedious plot options like font size and interpreter en masse.
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Adam Danz on 20 Nov 2019
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_769359
I wonder what's not working for the label font size when axis font size is set using set(gca,'fontsize',14)
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Adam Danz on 20 Nov 2019
⋮
Edited: Adam Danz on 14 Jun 2022
Open in MATLAB Online
Set axis fontsize
set(gca,'fontsize', 14)
The axis fontsize affects the title, axis labels, and axis tick labels, and any legends or colorbars associated with the axes.
fontsize function (R2022a and later)
This function allows users to set a uniform fontsize across all text in graphics object just as an axes or figure or you get set a scaling factor to increase/decrease fontsize while maintaing the relative differences of fontsize between text objects. Also see this Community Highlight.
If these solutions above are not what you're looking for, here are three alternatives that focus on the xlabel and ylabel font sizes.
Idea 1: put all shared name-value properties into a cell array
For sets of name-value pairs that will be assigned to several objects, you can group them into 1 variable and assign them like this.
extraInputs = {'interpreter','latex','fontsize',14}; % name, value pairs
xlabel('$k$',extraInputs{:})
ylabel('$h_1$',extraInputs{:})
legend({'closed loop','setpoint'},extraInputs{:})
Idea 2: set the axis properties when possible
Axes do not have an interpreter property but you could avoid assigning font size (and other properties) to each axis label and legend by assigning those properties to the axes.
set(gca,'fontsize',14)
xlabel('$k$','interpreter','latex')
ylabel('$h_1$','interpreter','latex')
legend({'closed loop','setpoint'},'interpreter','latex')
Idea 3: create a local function designed to produce formatted axes and labels
Lastly, if you're creating a bunch of subplots that all have the same set of properties, create a local function that creates subplots and assigns the formatted axis labels.
figure('DefaultAxesFontSize',14)
clf
ax = newsubplot(221,'k','h_1');
plot(1:5,'LineWidth',2)
line([0,1], [1,2],'color','red','linestyle','--','LineWidth',2)
legend({'closed loop','setpoint'},'interpreter','latex')
function ax = newsubplot(position, xlab, ylab)
% Creates new subplot in specified position on current figure
% with xlab xlabel and ylab ylabel
ax = subplot(position);
hold on
set(ax,'FontSize',14) %and other properties
xlabel(['$',xlab,'$'],'interpreter','latex')
ylabel(['$',ylab,'$'],'interpreter','latex')
grid on
end
7 Comments Show 5 older commentsHide 5 older comments
Show 5 older commentsHide 5 older comments
Morten Nissov on 20 Nov 2019
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_769363
Open in MATLAB Online
I think
set(gca,'fontsize',14)
didn't work because I called it after xlabel and ylabel instead of before like you do. Anyway thanks so much this is exactly what I was looking for.
Adam Danz on 20 Nov 2019
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_769368
Glad I could help tidy up some code!
Steven Lord on 20 Nov 2019
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_769390
Other options:
If you want to change the FontSize and Intepreter properties for all the text objects in your figure, you could set default property values at the level of groot or a particular figure to affect all text object created afterwards in that session (for groot) or in that figure.
Alternately, if you've already created text objects you could use findobj or findall to locate all the graphics objects with those specific properties or of a specific Type. Once you have the vector of handles to those objects you can change the values of those properties with set in one call.
Ilya Gurin on 27 Aug 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_1709299
Open in MATLAB Online
I'm looking for a hybrid of idea 2 and idea 3, like this:
set(gcf, 'fontsize', 14)
Is this possible?
Ilya Gurin on 27 Aug 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_1709304
Open in MATLAB Online
Seems to work, actually.
set(gcf(), 'DefaultAxesFontSize', 14)
But it seems to only affect subsequent plot commands, not any existing plots.
Adam Danz on 28 Aug 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_1709484
Use findobj to find all objects in a figure that have a font size property and then use the set command to set the font size to all of those objects. If you have trouble implementing that I can show you how.
Adam Danz on 14 Jun 2022
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_2215825
Answer updated 6/14/22 to include the axis fontsize property and the new fontsize function.
Sign in to comment.
More Answers (1)
Image Analyst on 14 Jun 2022
- axes_colors.m
For what it's worth, I'm attaching a demo to change almost anything you want in a graph by setting various properties.
Hope it helps somebody.
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Adam Danz on 14 Jun 2022
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/492150-changing-font-size-of-all-axes-labels#comment_2215875
Can't take my eyes off it! :)
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsGraphics ObjectsGraphics Object Programming
Find more on Graphics Object Programming 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office