MATLAB Answers

How do I change the font size for text in my figure?

9 563 views (last 30 days)
Edward on 26 May 2014
Commented: DN7 on 18 Dec 2020
I'm using "set(gca,'fontsize', 18);" in a function to change fonts in a figure. My code does not throw an error, but it also does not change the font size. I can manually change the fonts via the UI, but this is a slow process. I'm running MATLAB 2013a on RHEL6.5
I've also tried "set(gca,'FontSize', 18);" and specifying 'FontSize', 18 in title, xlabel, ylabel and legend. None of these have worked.
Please advise!


Show 7 older comments
voltmeter cav
voltmeter cav on 10 May 2020
Here is my lines. I am using 2014a version. if I put "set(gca... )" line in 6th line, it does not change the font. but if I put this to the end of lines it works.
1 t=0:0.1:6;
2 x=(t.^2)/2;
3 y=(t.^2)/4;
4 z=(t.^2)/8;
5 k=(t.^2)/16;
6 %set(gca,'Fontsize',20);
7 plot(t,x,'lineWidth',2);
8 grid on;
9 hold on;
10 plot(t,y,'lineWidth',2);
11 hold on
12 plot(t,z,'lineWidth',2);
13 hold on;
14 plot(t,k,'lineWidth',2);
15 xlabel('(Sn)','Fontsize',20);
16 ylabel('(J)','Fontsize',20);
17 legend({'L=1H','L=2H','L=4H','L=8H'},'Fontsize',20);
18 set(gca,'Fontsize',20);
Image Analyst
Image Analyst on 10 May 2020
Here is code that shows you how to change just about anything about the axes that you want:
% Demo to make a black graph with blue title, red Y axis, green X axis, and yellow grid.
% Initialization steps:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Create sample data.
X = 1 : 20;
Y = rand(1, 20);
plot(X, Y, 'gs-', 'LineWidth', 2, 'MarkerSize', 10);
grid on;
title('Y vs. X, Font Size 12', 'FontSize', 12, 'Color', 'b', 'FontWeight', 'bold');
% Make labels for the two axes.
xlabel('X Axis, Font Size 15');
ylabel('Y axis, Font Size 24');
yticks(0 : 0.2 : 1);
% Get handle to current axes.
ax = gca
% This sets background color to black.
ax.Color = 'k'
ax.YColor = 'r';
% Make the x axis dark green.
darkGreen = [0, 0.6, 0];
ax.XColor = darkGreen;
% Make the grid color yellow.
ax.GridColor = 'y';
ax.GridAlpha = 0.9; % Set's transparency of the grid.
% Set x and y font sizes.
ax.XAxis.FontSize = 15;
ax.YAxis.FontSize = 24;
% The below would set everything: title, x axis, y axis, and tick mark label font sizes.
% ax.FontSize = 34;
% Bold all labels.
ax.FontWeight = 'bold';
hold off
Alex Hruksa
Alex Hruksa on 28 Aug 2020
This bit of code is super useful, thanks Image Analyst!

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 27 May 2014
Usually you can set the font size on every control individually as you update its text, like
title('This is my plot', 'FontSize', 24);
xlabel('x axis', 'FontSize', 24);
text(x, y, 'Hey, look at this', 'FontSize', 24);
What's wrong with doing it like that? That's what I do.


Show 2 older comments
Image Analyst
Image Analyst on 29 May 2014
I don't know. That's bizarre. You should call tech support.
Peter on 27 Sep 2016
well, probably this font is not available in other sizes

Sign in to comment.

More Answers (8)

Mike Garrity
Mike Garrity on 10 Feb 2016
Yes, this can be confusing. Here's what you're probably seeing:
figure % Creates a figure
set(gca,'FontSize',18) % Creates an axes and sets its FontSize to 18
plot(x,y) % Resets the axes and plots into it
Notice the "Resets the axes" part. One of the things that happens there is that the FontSize property gets set to the default!
This doesn't happen when hold is on because then the axes doesn't get reset.
There are a couple of ways around this.
The simplest is to set the FontSize after plotting.
A somewhat more complicated way is to change the default:
Does that make sense?

  1 Comment

Rik on 9 Feb 2017
The point is that the font size property is inherited from the figure. So instead of calling gca, you should call gcf. But indeed, best practice is setting the font size on creation of the figure window.

Sign in to comment.

José Crespo Barrios
José Crespo Barrios on 10 Feb 2016


Ian Hunter
Ian Hunter on 19 Sep 2017
When I invoke this after a call to figure, there does not appear to be any change in text size.
ubaid haroon
ubaid haroon on 27 Jun 2019
I use this on unix R2015a and it seems to work for me

Sign in to comment.

Sean de Wolski
Sean de Wolski on 27 May 2014
I think what you want to do is set the 'Default' font size for the axes
Now any text object on that axes will have 18 font


Show 1 older comment
Image Analyst
Image Analyst on 11 Feb 2020
Or, since r2014b, you can do it without the set() function:
ax = gca;
ax.DefaultTextFontSize = 18;
DN7 on 18 Dec 2020
If gca does not work for you, make sure you didn't accidentally create a variable named that way. use:
clearvars gca
to ensure that. I accidentally created this variable (struct) when I ran gca.FontSize = 13, which does not change the font size of the current axis, but instead creates a new struct.

Sign in to comment.

Renato Campana
Renato Campana on 18 Nov 2017
Im working with Matlab 2016. You can tried two things:
1)figure('DefaultAxesFontSize',30); % here the font size is 30. figure (1) plot(x,y,'LineWidth',4); % note that the linewidth here is 4 xlabel('length bar','FontSize',18); % note that the font size label here is 18 ylabel('wide bar','FontSize',18); % note that the font size label here is 18
and you must to use the dame command figure('DefaultAxesFontSize',30) in each figure. If you dont specified the font size in each label, the labels shows the size in "30"
Or you can tried:
2) figure (1) plot(x,y,'LineWidth',4); set(gca,'FontSize',28); % please, note that the font size is AFTER the plot command :)

Anu on 1 Jan 2015
I have also encountered the same problem. I was using Linux Mint OS. I solved it by installing the xfont 100 and 75 dpi and the truetype fonts. Try it out once.


Sign in to comment.

Daniel on 26 Mar 2015
I just wanted to weigh in on this given I've spent the last couple of hours looking into this.
I am running Matlab 2013b on Ubuntu 12.04LTS. Similar as many here, changing labels/legend properties works fine but setting the axis ticklabel fontname/size was not working - at least, the axis property list reflected the change, but the window plot was not rendering to the new font settings. After printing the plot to eps and including this in my latex compiled document, it turns out the axis font properties were changing. It would appear to be just a rendering bug.
Installing additional fonts did not work for me - and I did not expect to, since rendering/changing font properties of other objects such as labels and legends worked fine in Matlab.
So for those of you cocnerned with the looks of your plots for publications, it would appear to me that the actual exported figures do reflect the editing (at least this was my experience when printing to .eps).


Sign in to comment.

vimal kumar chawda
vimal kumar chawda on 12 Aug 2020
plot(A_OBS(2).RxTime(:)/3600, No_ele2(1:r2, 1), '.b');
hold on;
plot(A_OBS(4).RxTime(:)/3600, No_ele4(1:r4, 1)-0.05, '.g');
xlabel('Time [h], Font size 15');
ylabel('Number of visible satellites,Font size 15');
title('Comparison between Javad and u-blox receivers (Gallileo)');
Why it is not working ?
I need to maximize the scale and the text in the axis scale.


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!