Why do I get "Ignoring extra legend entries" evein in such trivial case?

66 ビュー (過去 30 日間)
Francesco
Francesco 2024 年 5 月 28 日
移動済み: Steven Lord 2024 年 5 月 28 日
Hello everyone,
MATLAB never misses on destroying any confidence you have in you coding skills.
I am coding a relatively easy script for data analysis, where I would plot data with a legend. In the code, I loop a plot() function to stack lines and then I ask to display a legend. However, I get the classic error from MATLAB:
Warning: Ignoring extra legend entries.
> In legend>process_inputs (line 590)
In legend>make_legend (line 319)
In legend (line 263)
Without dumping here the entire code, which is redundant, I get the same warning in an analogous and much simpler case:
figure;
x = 1:10;
y = randn(5, 10);
Labs = ["1", "2", "3", "4", "5"];
for i = 1:5
plot(x, y(i,:)); hold on
end
legend(Labs)
The result is 5 lines in the plot, but MATLAB throws the warning above, displaying an empty legend in the figure.
If I try instead the "DislayName" method, the warning disappears, but the legend doesn't even show up.
figure;
x = 1:10;
y = randn(5, 10);
Labs = ["1", "2", "3", "4", "5"];
for i = 1:5
plot(x, y(i,:), "DisplayName", Labs{i}); hold on
end
legend
Can someone please help make sense of this? Thank you very much in advance.
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2024 年 5 月 28 日
"Without dumping here the entire code, which is redundant, I get the same warning in an analogous and much simpler case:"
That code runs without any error -
figure;
x = 1:10;
y = randn(5, 10);
Labs = ["1", "2", "3", "4", "5"];
for i = 1:5
plot(x, y(i,:)); hold on
end
legend(Labs)

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

採用された回答

Sayan
Sayan 2024 年 5 月 28 日
編集済み: Sayan 2024 年 5 月 28 日
Hi Francesco,
The warning appears only when the legend() function is called more number of times than that of the number of plot objects. The error could not be reproduced in your provided first sample code as there are 5 legend entries which is the same as the number of plot calls in your loop.
In your actual code you should find if there are calls to legend() more number of times than that of the number of lines plotted.
A similar kind of issue has been addressed in the following MATLAB answer.
Hope this helps in resolving the issue.

その他の回答 (1 件)

Francesco
Francesco 2024 年 5 月 28 日
移動済み: Steven Lord 2024 年 5 月 28 日
Dear Sayan,
thank you for your answer, which gave me the idea to check a few functions I have in my code which format plots with specific colours, fonts and graphics settings. Turns out that one of the default settings was:
set(groot,...
"DefaultLineHandleVisibility", "off")
which is used to avoid plotting graphic elements in very complex plots, where the data lines are a minority.
I did not realised that these settings were carried over to my data analysis plot.
Setting in the looped plot()
plot(x, y, "HandleVisibility", "on"); hold on;
solved the issue, which was indeed involving no lines plotted from the point of view of legend().
Thank you again.

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by