How do I change the font size in my legend?

3,000 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
編集済み: MathWorks Support Team 2023 年 4 月 18 日
How do I change the font size of the labels in my legend? For example, how do I make the font size of the ‘One’, ‘Two’, ‘Three’, ‘Four’ labels bigger in my plot?
plot(rand(4))
lgd=legend('One','Two','Three','Four');

採用された回答

MathWorks Support Team
MathWorks Support Team 2023 年 4 月 18 日
編集済み: MathWorks Support Team 2023 年 4 月 18 日
Starting in R2022a, you can use the fontsize function to change the font size within a figure or for a particular object, such as a legend. You can set the font size to a specific number, or you can incrementally increase/decrease the size of all the text for the object while maintaining relative size differences.   
For example, this code creates a plot and a legend and sets the legend's font size to 14 points. 
plot(rand(4)) 
lgd = legend('One','Two','Three','Four');
fontsize(lgd,14,'points')
This code decreases the font size by a factor of 0.9. 
fontsize(lgd,'decrease')
R2022a also includes the fontname function, which enables you to change all the fonts within the figure or for a particular object in the figure. 
For previous releases:
You can change the font size for a MATLAB legend by setting the 'FontSize' property of the Legend object.
For example, plot four lines. Create a legend and assign the Legend object to the variable 'lgd'. Then, use dot notation to access the 'FontSize' property and set the value to 14 points.
plot(rand(4))
lgd = legend('One','Two','Three','Four');
lgd.FontSize = 14;
Alternatively, you can specify the 'FontSize' property using a name-value pair argument in the 'legend' command. When including name-value pair arguments, include the legend labels in a cell array. For example:
legend({'One','Two','Three','Four'},'FontSize',14)
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 9 月 20 日
ali jebeli comments:
does not work
Walter Roberson
Walter Roberson 2017 年 9 月 20 日
ali jebeli: which MATLAB version are you using?

サインインしてコメントする。

その他の回答 (1 件)

Mukesh Jadhav
Mukesh Jadhav 2016 年 10 月 9 日
Here you go :
h = legend('x','y','z');
set(h,'FontSize',12);

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by