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

조회 수: 7 (최근 30일)
Drew Chap
Drew Chap 2022년 7월 14일
답변: Shubham 2023년 8월 30일
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.

답변 (1개)

Shubham
Shubham 2023년 8월 30일
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!

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by