Changing font size of all axes labels

조회 수: 1,719 (최근 30일)
Morten Nissov
Morten Nissov 2019년 11월 20일
댓글: Adam Danz 2022년 6월 14일
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
Adam Danz
Adam Danz 2019년 11월 20일
I wonder what's not working for the label font size when axis font size is set using set(gca,'fontsize',14)

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 11월 20일
편집: Adam Danz 2022년 6월 14일
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
Adam Danz
Adam Danz 2021년 8월 28일
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
Adam Danz 2022년 6월 14일
Answer updated 6/14/22 to include the axis fontsize property and the new fontsize function.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 6월 14일
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.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by