Hide MATLAB legend entries for some graphical objects on graphs

The MATLAB traditions list everything in the plot, including the recommendations that you pointed to the plot.

Fiction get around this to do

*Plot
*Add legend
*Add guidelines

However, MATLAB puts the very last lines in front, that is, the guidelines then sit on the displayed data; ugly and distracting.

Similar problems arise at any time when you create a complex plot, legenddistracting and capturing everything, and workarounds with building order can be ugly

Code example:

%**** Optional guidelines
figure(1)
plot([2 2],[0,1],'k--'); hold on

%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;
for plotLoop=1:N;
  %* Plot
  figure(1)
  plot(x,y(plotLoop,:));
  hold on
end

%*****LEGEND
hLegend = legend(LegTxt,...
                'interpreter','latex',...
                'location','eastoutside')

(reorder the code block order to reproduce the situations mentioned above)

How to reasonably fix this?

+4
source share
2 answers

, ( , ), LegendInformation:

%# plot something that shouldn't show up as legend
handleWithoutLegend = plot(something);

%# modify the LegendInformation of the Annotation-Property of the graphical object
set(get(get(handleWithoutLegend,'Annotation'),'LegendInformation'),...
    'IconDisplayStyle','off');

%# toggle legend on and off at will, and never see the something-object appear

, - try-wrapper , :

for h = listOfHandles(:)'
   try
      set(get(get(h,'Annotation'),'LegendInformation'),...
        'IconDisplayStyle','off');
   end
end
+6

, legend. , , .

, .

%**** Optional guidelines for periodicity
figure(1)
plot([2 2],[0,1],'k--'); hold on

%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;

for plotLoop=1:N;
  LegTxt{plotLoop} = num2str(plotLoop);
  %* Plot
  figure(1)

  % if statement to construct a handle for the legend later
  if plotLoop==1 
      htot=plot(x,y(plotLoop,:));
  else
      h=plot(x,y(plotLoop,:));
      % Append this info to the figure handle
      htot= [htot, h];
  end
  hold on

end

%*****LEGEND
hLegend = legend(htot,LegTxt,...
                'interpreter','latex','FontSize',16,...
                'location','eastoutside')

, for plotLoop=1:N; , , . , , !

+3

Source: https://habr.com/ru/post/1670878/


All Articles