Split legend of figure into multiple columns ?

31 ビュー (過去 30 日間)
Aravin
Aravin 2012 年 1 月 1 日
コメント済み: Marcus Oliveira 2021 年 2 月 22 日
Dear All,
I have one figure which have many curves. I can't show my legend inside the figure so I have to show them outside. There are two possible layouts(orientation) of legends:
  1. Vertical
  2. Horizontal
In both orientation, legend gets too big to display. It can be displayed properly if and only if I could split legend into multiple columns. Like in first column I show 10 curves name and in second column I show next 10. I could not figure it either it is possible or not?
Any suggestion will be highly appreciated.

採用された回答

Jan
Jan 2012 年 1 月 1 日
See:
Lookinf in the FileExschange is always a good idea.
  1 件のコメント
Aravin
Aravin 2012 年 1 月 1 日
Thank you so much. This is exactly what I wanted :-)

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

その他の回答 (3 件)

MattF
MattF 2017 年 8 月 25 日
編集済み: MattF 2017 年 8 月 25 日
Hi!
I was also using columnlegend which is a great tool. However, I found myself another solution that might be useful to somebody and I will give you a short example here for a two rows legend:
Let's say you define your plots as array like this
h(i) = plot(x,y,<options>);
And your legend as cell
legendInfo = {'item1','item2',...,'itemN'};
Then you can define a legend object as follows
hL1 = legend(h(1:l), legendInfo(1:l), <options>);
hL2 = legend(h(l+1:end), legendInfo(l+1:end), <options>);
where in options you might be willing to set e.g. the orientation as 'horizontal'. Then you can define a position and the units for this
newPosition1 = [a b c d];
newPosition2 = [e f g h];
newUnits = 'normalized';
where actually 'c' and 'd' are the same as 'g' and 'h'. Eventually, you can set your legend as
set(hL1, 'Position', newPosition1, 'Units', newUnits);
set(hL2, 'Position', newPosition2, 'Units', newUnits);
  1 件のコメント
Marcus Oliveira
Marcus Oliveira 2021 年 2 月 22 日
Thanks, MattF!
How could I write these lines in a single box?

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


Jessica
Jessica 2019 年 1 月 4 日
MattF - thanks for offering this alternate solution! I'm using Matlab version 2018a, and it doesn't seem to work for me.
When I create the first legend, hL1 has type matlab.graphics.illustration.Legend. Then when I create hL2, it has the same type, and appears to simply overwrite hL1, even though both stay in my variable space.
I then tried to assign them different locations, but the last-created legend just moves around - the first legend appears to have been over-written. I'm guessing that this is a Matlab version issue, but wanted to know if I was missing something, perhaps in those <options>.
  3 件のコメント
ahmad eldeeb
ahmad eldeeb 2019 年 1 月 30 日
It doesn't work for 2017b but works for 2018b with me as :
set(legend, 'NumColumns' ,3)
Praveen Teleti
Praveen Teleti 2019 年 4 月 10 日
To avoid first legend object being overwritten, save a copy of figure object just before you create second legend object, example:
hL1 = legend(h(1:l), legendInfo(1:l), <options>);
set(hL1, 'Position', [0.7 0.4 0.1 0.4]);
a=axes('position',get(gca,'position'),'visible','off');
hL2 = legend(a,h(l+1:end), legendInfo(l+1:end), <options>);
set(hL2, 'Position', [0.78 0.4 0.1 0.4]);
This process can be repeated any number of times to create any number of legend objects. Hope it helps!

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


Camilo Malagon Nieto
Camilo Malagon Nieto 2017 年 8 月 14 日
Can some one explain this commands better... because for example the columnlegend gives "Undefined function 'columnlegend' for input arguments of type 'cell'." ????
  1 件のコメント
Jan
Jan 2017 年 8 月 14 日
This is not a problem of the command, but you did not install in in a folder, which is contained in your Matlab path. See:
help addpath

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

カテゴリ

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