How to prevent the legend from updating in R2017a and newer?

前端 未结 1 1855
天命终不由人
天命终不由人 2021-01-07 03:05

Since MATLAB R2017a, figure legends update automatically when adding a plot to axes. Previously, one could do this:



        
相关标签:
1条回答
  • 2021-01-07 03:31

    The release notes for MATLAB R2017a mention this change, and provide 4 different ways of handling the situation. These two methods are easiest to put into existing code:


    1: Turn off auto updating for the legend before adding the black line. This can be done at creation time:

    legend({'line1','line2','line3','line4'}, 'AutoUpdate','off')
    

    or after:

    h = findobj(gcf,'type','legend');
    set(h, 'AutoUpdate','off')
    

    You can also change the default for all future legends:

    set(groot,'defaultLegendAutoUpdate','off')
    

    2: Turn off handle visibility for the black line that you don't want added to the legend:

    plot([1,100],[0,0],'k-', 'HandleVisibility','off')
    

    The IconDisplayStyle method is also shown here. However, they use the dot notation, which makes the syntax is a bit prettier:

    h = plot([1,100],[0,0],'k-'); % keep a handle to the added line
    h.Annotation.LegendInformation.IconDisplayStyle = 'off';
    
    0 讨论(0)
提交回复
热议问题