MATLAB Answers

Karen
0

In 2017a, How do I prevent a legend item from being 'greyed out' when the plot line is set with visibility off?

Asked by Karen
on 16 Aug 2017
Latest activity Commented on by Karen
on 23 Aug 2017
With 2017a, my legend entries are showing up as 'greyed out' when the plot object is set with visibility off. This is not the behavior for R2015b (and previous releases).
This example code shows the legend differently in R12015b vs. R2017a. I want the R2015b behavior. I frequently use this approach when I want the legend to be in one order, but the plotting order to be different.
%%Example of greyed out legend.
% allows order of legend to be different from plotting order
% 2015b: all legend entries are normal color
% 2017a: 1st legend entry has gray text and the plot line/symbol is greyed
figure
legstr = {};
hold all;
plot(0,0,'ro-','Visible','off')
legstr(end+1) = {'Line'};
plot(1:100,randn(1,100),'ks-')
legstr(end+1) = {'Random'};
plot([1 100],[0.5 0.5],'ro-')
lh=legend(legstr);
I'd prefer a solution that doesn't require rewriting 100's of lines of existing code, like a property setting in the legend command.
Thanks

  0 Comments

Sign in to comment.

1 Answer

Answer by Vikaasa Kumar on 21 Aug 2017
 Accepted Answer

Hi Karen,
The behavior of dimming out a legend entry if its respective line's visibility is turned off was changed in R2016a. Currently, there is no way of disabling this behavior. I apologize for the inconvenience.
As a workaround, you could plot using NaN's so that the legend entry is not grayed out. This is depicted in the following example:
The code below will have the legend entry grayed out.
figure
axes
l = line(1,1)
l.Marker = 'o'
l.Visible = 'off'
legend(gca,'aa')
Plotting with NaN's will avoid that behavior:
l = line(nan,nan,'Marker','o');
legend show;
This way, the line is not shown, but it is still treated as visible so the legend entry is not grayed out.
I hope this information was helpful.
Have a nice day!
Best,
Vikaasa

  2 Comments

Thanks Vikaasa-
Plotting NaN's works.. I had tried a similar method of plotting a point that wouldn't be within view, but it wasn't robust when the axis limits weren't defined. This workaround isn't too difficult to implement in my plotting scripts.
karen

Sign in to comment.