Skip empty array in plot legend?

6 visualizaciones (últimos 30 días)
David Stolnis
David Stolnis el 12 de Abr. de 2019
Comentada: MattyK el 5 de Nov. de 2024
I'm using logical arrays to plot various levels of threshold excedance.
How can I skip legend titles of empty arrays? In the following screenshots you'll see all levels of threholds exceeded, next you will see where only low level of thresholds are exceeded. and the legend is incorrect. As a note on the second figure, the yellow dots shold be "exceeds construction lims".
The bottom plots legend should have "exceeds constuction lims" instead of "Level1"
I'm assingin variable names to each plot and using hold on. Simplified example code:
a = plot(the blue line);
b = plot (logical array of level 1);
c = plot(logical array of level 2);
d = plot(logical array of exceeds construction lims);
legend([a b c d], {'blue line', level1', 'level2', 'exceed const lims'});
I need to be able top skip level1 and level2 if they are 0x1 empty arrays.
I cannot simply change the order because for somethings I do not have constuction limits so I would run into the ame issue by just reordering my plots.

Respuestas (2)

Adam Danz
Adam Danz el 12 de Abr. de 2019
Editada: Adam Danz el 12 de Abr. de 2019
Try this.
% List all possible legend handles and their strings
legHands = {a b c d};
legStrs = {'blue line', level1', 'level2', 'exceed const lims'};
% Determine which handles aren't empty
hasData = ~cellfun(@isempty, legHands);
% Produce legend, ignore empties
legend([legHands{hasData}], legStrs{hasData})
  1 comentario
MattyK
MattyK el 5 de Nov. de 2024
Thanks very much for this, it works wonders

Iniciar sesión para comentar.


Matt J
Matt J el 12 de Abr. de 2019
Editada: Matt J el 12 de Abr. de 2019
Why not something like this?
allLines=[a,b,c,d];
allStrings={'blue line', 'level1', 'level2', 'exceed const lims'};
legend(allLines(subset), allStrings(subset))
where subset is a logical array based on your inclusion criteria for the legend.

Categorías

Más información sobre Legend en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by