How do I change the font size for text in my figure?
6,600 views (last 30 days)
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.
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.
More Answers (11)
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?
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
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).
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.
Adam Danz on 18 Mar 2022
Starting in MATLAB R2022a, use the fontsize function to scale font sizes and set font units in a figure. You can set a fontsize for all objects in a figure or incrementally increase/decrease fontsize while maintaining relative differences in fontsize.
Release R2022a also includes the new fontname function to set font names within a figure.
For a review, see this Community Highlight.
vimal kumar chawda on 12 Aug 2020
plot(A_OBS(2).RxTime(:)/3600, No_ele2(1:r2, 1), '.b');
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.
Eitvydas Karauskas on 4 Apr 2021
Hey guys, I have a different problem with text function. Why does my text size changes when I zoom in or out of my graph? I need to put text at a fixed sized, so it doesn't change when I zoom in or out. I'm adding my code.
Thanks for help ;)
%defining latitude and longitude
%loading world map
%loading lithuania borders from external source and displaying it as a
country=shaperead('gadm36_LTU_0.shp','usegeocoords',true,'BoundingBox', [lonlim', latlim']);
geoshow(map,country,'facecolor',[1 1 1],'linewidth',2);
%converting coordinates to lat/lon
VNOlon = 25.293639;
VNOlat = 54.636056;
geoshow( VNOlat, VNOlon, 'marker','.', 'markerfacecolor','blue','markeredgecolor','blue','markersize',6);
%converting km to nn and defining radius
r = [ 9.26 18.52 27.78];
%defining radius from VNO
textm(54.65831449445, 25.14947845266, '5nm VNO','rotation',70,'fontsize',3,'fontunits','normalized');
Michael Neely on 20 Nov 2021
None of the above answers on using a single line to "set gca" or "set default" actually work. I have had success setting the fonts in individual print commands (as some of the answers suggest). However, it is quite annoying that there is no simple way to put one line that changes the font size for everything.
The only way I can do it is to physically operate on the figure itself, clicking and enlarging, and this seems to be highly dependent on the size of my window that I use to display the figure. If I physically enlarge the window then the fonts might be modified. This makes it very difficult to consistently print figures for a publication. Some figures will have font sizes slightly different than others.