How can I change the font size of tick labels on the axes of a graph?

I have tried all of the suggestions in fx, Help and answers to other people's questions, but I cannot make any of them work in Matlab 2020a.

Answers (2)

It's in the documentation under axes properties:
plot(1:5)
ax=gca;
ax.FontSize = 20
You can also change a single axis by specifying that axis:
ax.XAxis.FontSize = 20

5 Comments

Chris,
Many thanks for responding.
If I run your code as the first script on opening Matlab, it works. If I incorporate your code into my script, or if I run your code after running mine, it does not work, leaving the font size as 12pt.
If I run my script, type clear into the command line and then run your code, it works. Instead of typing clear, I have tried typing close all, and I have tried deleting the variable ax, both without success. I have also tried “commenting out” every line containing “ax.” in my script, but without success. In my script, I can control the font size of the titles, axis titles and legends of the graphs, but not the size of the tick labels.
When your code works, it responds with the message:
ax =
Axes with properties:
XLim: [1.0000e+000 5.0000e+000]
YLim: [1.0000e+000 5.0000e+000]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [130.0000e-003 110.0000e-003 775.0000e-003 815.0000e-003]
Units: 'normalized'
Show all properties
When it does not work, the message is different:
ax =
struct with fields:
Legend: [1×1 struct]
FontSize: 20.0000e+000
I would be most grateful for any further thoughts that you may have. I have sent you an email with my script attached, in case it is useful to you, in the hope that you may be able to find the part of it that is preventing me from changing the font size of the tick labels. I was able to change the font size of tick labels when using earlier versions of Matlab, but I have also changed my script many times for each time I have updated the version of Matlab. Because my script is rather large and it takes a while to run, I have also attached a version of your code, with just one of the graphs that my script produces, as the file FontSize.m; this file runs successfully to produce both my graph and yours with large font tick labels, provided that it is not preceded by my full script, or it is preceded by a clear instruction.
Thanks again for your response. I look forward to hearing from you.
David.
Cris,
I did email my full script to you on Saturday, in reply to your email, but it appears not to have reached you. It is now attached to this comment.
Thanks for your continuing help and support.
David.
You likely replied to an email notification from MATLAB Central. Replies are not actually directed to anyone.
I've poked around in your code. I didn't attempt to explore every plot, but if I were you, I would probably set the properties this way.
title(strcat('Basilar Membrane Velocity Transfer Function, Excitation @',Stringt,sprintf('\n Frequency = %0.0f Hz,',f(fn)),Comp))
ylabel(strcat('Basilar Membrane velocity amplitude (dB)',Stringy))
xlabel('Distance from Stapes (mm)')%lgd.Title.String = 'Frequency (kHz)';
ax=gca;
ax.XAxis.FontSize = 12;
ax.YAxis.FontSize = 12;
ax.XLabel.FontSize = 24;
ax.YLabel.FontSize = 14;
ax.Title.FontSize = 18;
Was there a particular plot that was not working?
Dear Cris,
your solution worked for me too.
Thanks for your brilliant help.
Best!

Sign in to comment.

Cris,
Thanks for your response. Your suggested way of specifying font sizes is much more rational than mine, and so I have tried it both for one plot in my full script and for the simplified script below. Running the script after executing a clear command results in your graph with 20pt tick labels, followed by my graph still with 12pt tick labels and no title or axis titles. Any thoughts?
figure(1)
plot(1:5 )
ax=gca ;
ax.FontSize = 20
f=logspace(log10(20),log10(20000),100);
HL0=linspace(0,50);
figure(6)
title('Measured and Predicted Hearing Loss (Reduction in BM Velocity) with no Bubble')
ylabel('Post Implant Hearing Loss (dB)')
xlabel('Frequency (Hz)')
ax=gca;
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
ax.XLabel.FontSize = 20;
ax.YLabel.FontSize = 20;
ax.Title.FontSize = 36;
semilogx(f,20*log10(HL0),'LineWidth', 3,'color','r')
grid on
There are two graphs that particularly need a larger font for the tick labels: figures 6 and 7 in my full script. I have deleted the many sections that create the other graphs, and I have attached the result. (To reduce the run time, I have also reduced the number of iterations necessary in the sections that do the calculations; this makes the graphs a bit more notchy, but it makes no difference to font sizes.)
David.

5 Comments

Order matters. Looking at your simple example, if you step through the code line by line, you see that the title, axis labels, and tick labels all get added as prescribed. Here, a break point was added at line 8 and I have stepped through the code line by line to line 18. Execution is paused at the green line.
In the next image, I have executed line 18, adding the semilog plot. As you can see, the semilogx plot command resets all the axes properties.
My recommendation - plot first, then modify the properties.
figure(1)
plot(1:5)
ax=gca;
ax.FontSize = 20
f=logspace(log10(20),log10(20000),100);
HL0=linspace(0,50);
figure(6)
semilogx(f,20*log10(HL0),'LineWidth', 3,'color','r')
title('Measured and Predicted Hearing Loss (Reduction in BM Velocity) with no Bubble')
ylabel('Post Implant Hearing Loss (dB)')
xlabel('Frequency (Hz)')
ax=gca;
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
ax.XLabel.FontSize = 20;
ax.YLabel.FontSize = 20;
ax.Title.FontSize = 36;
grid on
Cris,
Your suggested script with the commands in the correct order works fine, if I run it either before running my full script or after a clear command. However, if I run it after my full script without a clear command, then all the fonts are small, including those of the tick labels. To see this for yourself, you could run my full script, close all, and then run your suggested script.
I have also tried correcting the order of the commands for one graph in my full script, and all the fonts are small on that graph.
Any thoughts?
David.
Cris,
Since adding my previous comment on 4th June, I have had to give priority to a couple of other things, but I have now incorporated your suggested improved order into the attached version of my "full" script. I have also deleted all other code that generates graphics, except that which plots the first line in figure 6. After running the script, the plot has 12pt font size everywhere, but multiplied by the scaling factors for titles, as normal. I note that there is a variable named ax, which shows the font sizes as set by your code, despite the graph's being produced with a 12pt font size throughout. Is there anything else in the script that could inhibit the implementation of the code that sets the font size?
David
Cris,
I haven't heard from you in a while; do I need to open a new question, or do you still have this in hand?
David.
Cris,
Because I have heard nothing further from you or anyone at Mathsoft in weeks, I had to try to find the problem myself by trial and error. Mathsoft and/or its customers may be interested in what I have found. The problem appears to be with the "load" function. If I load the workspace with a .mat file, this prevents the script from changing the font sizes. If I delete the "load" command from the script, then the code that you suggested and the code that I was using previously both work. I can manage without using the "load" function, but doing so is inconvenient, and it will become more so as my project develops. Am I doing something wrong, or is this a bug?
Please respond to this, even if only to ask me to open a new question.
David.

Sign in to comment.

Products

Asked:

on 29 May 2020

Community Treasure Hunt

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

Start Hunting!