How can I change the default legend font independently of the default axes font?

7 views (last 30 days)
I would like to set the default legend font properties independently of the default axes font properties. For example, in my startup.m file I have tried the following:
set(0,'defaultAxesFontName','Consolas')
set(0,'defaultAxesFontSize',16)
set(0,'defaultLegendFontName','Helvetica')
set(0,'defaultLegendFontSize',14)
But the 3rd and 4th lines don't appear to have any effect, the legend font name and size are still set by those first two lines.

Answers (1)

Shubham
Shubham on 30 Aug 2023
Hey Drew Chap,
Here is a working code for setting the default font properties for both axes and legend and plotting two figures in the same session to test it out. Have a look at the following code:
% Change default font for the axes
set(groot, 'DefaultAxesFontName', 'Consolas', 'DefaultAxesFontSize', 40);
% Change default font for the legend
% set(groot, 'DefaultLegendFontName', 'Helvetica', 'DefaultLegendFontSize', 5);
set(groot, 'DefaultLegendFontName', 'Ariel','DefaultLegendFontSize',5,'DefaultLegendFontSizeMode','manual');
% Generate some sample data
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
% Create the plot
figure;
plot(x, y1, 'b', 'LineWidth', 2);
hold on;
plot(x, y2, 'r', 'LineWidth', 2);
% Add legend
legend('Sin(x)', 'Cos(x)');
% legend('Sin(x)', 'Cos(x)', 'FontName', 'Helvetica', 'FontSize', 5);
% Add labels and title
xlabel('x');
ylabel('y');
title('Simple Plot');
% second plot for testing
figure;
plot(1:10,1:10);
hold on;
plot(1:10,2:2:20);
legend('a','b');
xlabel("c");
You can also refer to this:
Hope this helps!

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!