MATLAB Answers

How to make all the legend appear in a plot?

28 ビュー (過去 30 日間)
Philippe Corner
Philippe Corner 2018 年 2 月 18 日
回答済み: Thomas Rutten 2018 年 12 月 5 日
Im using this code to plot X vector vs all the columns of the table on example.txt
C = readtable('example.txt', 'Delimiter',',');
X = 5:5:430;
for i=1:numel(X)
plot(X,C{:,i})
hold on
grid on
end
LegendString = cell(1,numel(X));
hold all
for k = 1:numel(X)
LegendString{k} = sprintf('%i',X(k));
end
legend(LegendString)
Why the plot is only showing me 50 legends and not all the 86 the code is asking?

  3 件のコメント

Jan Kappen
Jan Kappen 2018 年 5 月 4 日
I have the same problem and in my case it makes sense to plot more than 50 legend entries. Why is there a hard coded limit of 50 rows, especially since you can use multiple columns now?
I believe the problem is in C:\Program Files\MATLAB\R2018a\toolbox\matlab\scribe\+matlab\+graphics\+illustration\+internal\generateDisplayNames.p (btw, I hate these pcoded files, what is so important about that files that it needs to be hidden?!), because after even changing line 629ff in legend.m to
if auto_children && length(ch) > 200
% only automatically add first 50 to cut down on huge lists
ch = ch(1:200);
end
and recompiling it, only 50 lines will be shown.
Any workaround?
Walter Roberson
Walter Roberson 2018 年 5 月 4 日
auto_children does not apply at all if you pass in the handles of the items to legend: it only applies if you rely upon legend() to find the graphics objects for you.
Jan Kappen
Jan Kappen 2018 年 5 月 5 日
Thanks for your answer! I made sure, this line hit. But it seems like the class matlab.graphics.illustration.Legend does this entry clipping when the property "Axes" is set, see line 340 in legend.m (R2018a). Probably during that assignment, the Property "String" of the legend will be set and limited to exactly the number plots in the Axes, but to max 50.
matlab.graphics.illustration.Legend is pcoded, so this is where the search ends. Very frustrating.

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 2 月 18 日
You can set the legend font size smaller, and you can set the legend Position in figure coordinates (but be aware that if you set Position then it will not automatically resize the axes to make room for it.)
However, you have a limited height or width to draw the legend into, and no matter how small you set the font, there will come a point where you have so many entries in the legend that they cannot fit in the allocated size. You should be considering whether it is reasonable to try to fit that much information into a legend().
I know that there are plans to try to improve legend layouts, but any such improvements will continue to be plagued with the problem of people trying to put too much information into a single graph.

  2 件のコメント

Philippe Corner
Philippe Corner 2018 年 3 月 23 日
Mr Walter, could you check this question please? any help-advise would be great! https://la.mathworks.com/matlabcentral/answers/390255-how-to-modify-the-code-changing-the-intervals
Walter Roberson
Walter Roberson 2018 年 3 月 23 日
Note: R2018a added the ability to put the legend into multiple columns.

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

その他の回答 (1 件)

Thomas Rutten
Thomas Rutten 2018 年 12 月 5 日
I have exactly the same problem with having only 50 legends at maximum in a plot. It is a decision for users to plot a limited number of legends and not to Matlab. Of course, many legends will make graphics rather confusing, but I have circumstances that I need more than 50 legends. The two column option doesn't solve that, because it is still limited to 50.
Another option (set(gcf,'NextPlot','add'), newP = copyobj(c,newAx);etc.), which Walter showed somewhere else, works to show all legends, but the highlighting of Markers-Lines is limlited to the 'active' first 50 legends.
So Matlab, please solve this limitation problem of 50 legends (like it was in the past) and let users decide how many legends they want to project.

  0 件のコメント

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by