Force a legend entry even if there are no matching elements in the plot
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Osh
am 26 Jan. 2016
Kommentiert: Osh
am 26 Jan. 2016
Hi,
I'm generating a plot based on a condition as follows:
d1 = plot(find(Vec == 1), Y(Vec == 1), 'r*', 'DisplayName', 'Data 1');
hold on;
d2 = plot(find(Vec == 2), Y(Vec == 2), 'b*', 'DisplayName', 'Data 2');
d3 = plot(find(Vec== 3), Y(Vec == 3), 'k*', 'DisplayName', 'Data 3');
legend([d1 d2 d3]);
Sometimes the array 'Vec' does not have entries matching the value 3. So there are no points generated with a black marker (k*) and the legend will not show 'Data 3'.
But I still want to force the legend entry to display that the Black points(if any) associate with DisplayName 'Data 3'. How do I achieve this?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Jan. 2016
d1 = plot([find(Vec == 1),nan], [Y(Vec == 1),nan], 'r*', 'DisplayName', 'Data 1');
hold on;
d2 = plot([find(Vec == 2),nan], [Y(Vec == 2),nan], 'b*', 'DisplayName', 'Data 2');
d3 = plot([find(Vec== 3),nan], [Y(Vec == 3),nan], 'k*', 'DisplayName', 'Data 3');
This ensures that every selection has at least a nan in the coordiantes rather than being empty. Nothing will be displayed for the nan (this is well defined in the graphics system and relied upon in multiple routines, not something that is going to change any year soon.) But the legend will be displayed because the line exists.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!