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

6 ビュー (過去 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 Center および File ExchangeLegend についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by