How can I enlarge the legend box-size?

460 ビュー (過去 30 日間)
Naveen
Naveen 2013 年 2 月 14 日
コメント済み: Marya Sadki 2022 年 2 月 15 日
Hi,
I am plotting two curves and defining their parameters using the legend. The code is as follows
hleg1 = legend ( ['$ n/n_c = $' num2str(den1)], ['$ n/n_c = $' num2str(den2)] );
set(hleg1,'Interpreter','latex')
set(gca,'fontsize',15)
My problem is that as soon as I change the font size to 15 the box around the legends doesn't expand automatically and I have legends of the curves going outside the legend box. Can anybody suggest something?

採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 14 日
legend() is implemented by creating a new axis, the handle of which is returned. You should be setting Interpreter and fontsize for the text entries rather than the axis.
[hleg1, hobj1] = legend( ...);
textobj = findobj(hobj1, 'type', 'text');
set(textobj, 'Interpreter', 'latex', 'fontsize', 15);
Then if you find you need to, set() the Position property of hleg1.
  2 件のコメント
Naveen
Naveen 2013 年 2 月 14 日
Thanks! It seems to work and it's a bit faster than the previous method I was trying. I don't know why? However I have another question. I have two legends and I could make it work as follows
[hobj1 hobj2] = legend ( ['$ n/n_c = $' num2str(den1)], ['$ n/n_c = $' num2str(den2)] );
textobj1 = findobj( hobj1,'type', 'text' );
set( textobj1, 'Interpreter', 'latex', 'fontsize', 15 );
textobj2 = findobj(hobj2, 'type', 'text');
set(textobj2, 'Interpreter', 'latex', 'fontsize', 15);
set(gca,'fontsize',15)
Is there any way that I don't have to define two textobj1 and textobj2 and set property for each one? I tried naively in the same way as I defined [hobj1 hobj2] but that didn't work.
Walter Roberson
Walter Roberson 2013 年 2 月 15 日
You have a legend with two entries. The second value returned by legend() is the handles to the objects that went up to make the legend, including the text objects. The findobj() reduces the list down to just the text objects. The set() then affects all of those text objects. So already this code works for legends with multiple entries.

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 14 日
編集済み: Azzi Abdelmalek 2013 年 2 月 14 日
set(hleg1,'position',[0 0 0.2 0.2])
In general
set(hleg1,'position',[x0 y0 width height])
  2 件のコメント
Naveen
Naveen 2013 年 2 月 15 日
Thanks a lot! Your methods is really robust, though I had to experiment with coordinates a bit, but it works beautifully now!
Marya Sadki
Marya Sadki 2022 年 2 月 15 日
Thanks, that what i search for.

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


Max Riedel
Max Riedel 2016 年 4 月 6 日
A simple workaround is to put additional spacing after the text.
E.g., by using \quad:
hleg1 = legend ( ['$ n/n_c = $' num2str(den1) '$\quad$'], ['$ n/n_c = $' num2str(den2) '$\quad$'] );

カテゴリ

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