# Why does my legend only display 50 entries?

62 ビュー (過去 30 日間)
MathWorks Support Team 2017 年 10 月 20 日
コメント済み: Dev-iL 2020 年 12 月 24 日
I have a plot with more than 50 items and I would like to add a legend for all of them. The legend gets truncated at 50.
p = plot(magic(100));
labels = cellstr(num2str((1:100)'));
legend(p,labels)

### 採用された回答

MathWorks Support Team 2017 年 10 月 20 日
Legends are currently limited to no more than 50 entries. Usually in plots with more than 50 features, the plot is so cluttered and the legend is so large that it is more advisable to select just a few key items to display in the legend.
If you do require more than 50 entries, the workaround is to create an additional legend. This requires creating an additional hidden axes.
First plot the original data.
p = plot(magic(100));
labels = cellstr(num2str((1:100)'));
legend(p(1:50),labels(1:50),'location','westoutside')
Next create an additional axes so that the figure can have two legends. Because the new legend needs information about the color of each line, it is necessary to copy the original lines to this new axes.
set(gcf,'NextPlot','add')
newAx = axes;
newP = copyobj(p,newAx);
legend(newP(51:100),labels(51:100),'location','eastoutside')
Now make the new axes and copied lines invisible. Normally, this would be done by setting the Visible property of each line to 'off', however this would gray out the corresponding entries in the legend. Instead, set the XData and YData properties of each line to NaN.
axis(newAx,'off')
set(newP,'XData',NaN,'YData',NaN)
##### 1 件のコメント表示非表示 なし
Eric Sargent 2020 年 12 月 9 日
Legend will cap the number of entries at 50 if no handles are specified. To have all objects in an axes show up, pass in their handles to the legend command.
p = plot(magic(100));
legend(p);

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

### その他の回答 (3 件)

Richard Mittleman 2019 年 9 月 6 日
This is an amazingly stupid way to have to do this
##### 6 件のコメント表示非表示 5 件の古いコメント
Walter Roberson 2020 年 9 月 16 日
You are right, I made a multiplication mistake.

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

Eric Sargent 2020 年 12 月 9 日
Legend will cap the number of entries at 50 if no handles are specified. To have all objects in an axes show up, pass in their handles to the legend command.
p = plot(magic(100));
legend(p);
##### 3 件のコメント表示非表示 2 件の古いコメント
Dev-iL 2020 年 12 月 24 日
Thank you for pointing this out! This indicates a different problem, where the legend(target, ___) syntax has side effects that change the outcome of the funciton significantly. At the very least it's a documentation bug, but more likely a sign that the legend implementation is somewhat flawed.

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

Dev-iL 2020 年 12 月 24 日
I suggest using the following undocumented feature for this:
hF = figure();
hAx = axes(hF);
plot(hAx, magic(100));
hL = legend(hAx, '-DynamicLegend');
set(hL, 'LimitMaxLegendEntries', false); % << Solution

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

### カテゴリ

Find more on Legend in Help Center and File Exchange

タグが未入力です。

R2017a

### Community Treasure Hunt

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

Start Hunting!

Translated by