To put legend selectively in a plot in Matlab with visibility control, you can follow these steps:
legend
function with all the line handles and legend titles as inputs. Use ,'AutoUpdate','off'
to prevent automatic updates of the legend.set
function with the 'Visible'
property to selectively hide/show certain legend entries.Here is an example code:
main.m672 chars22 lines
In this example, we create a plot with y1
, y2
, and y3
lines using different styles. Then, we add a legend with all entries and turn off automatic updates. Finally, we use set
function to selectively hide all entries and show only y1
and y2
. We refresh and update the legend with 'show'
option.
gistlibby LogSnag