隐藏图中某些图形对象的MATLAB图例条目 [英] Hide MATLAB legend entries for some graphical objects in plots

查看:894
本文介绍了隐藏图中某些图形对象的MATLAB图例条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB图例列出了绘图中的所有内容,包括放置在绘图上的准则.

MATLAB legends list everything in a plot, including guidelines that you have put on a plot.

解决这个问题的软糖

*Plot
*Add legend
*Add guidelines

但是,MATLAB将最新的行放在最前面,这意味着准则随后位于显示的数据之上;丑陋和分散注意力.

However, MATLAB puts the most recent lines in the front, meaning the guidelines then sit over the displayed data; ugly and distracting.

类似的问题会在您构建复杂的图块时出现,legend吓跑并抢走所有东西,并且绘制顺序的变通办法可能很丑

Similar problems occur any time you build up a complicated plot, legend freaks out and grabs everything, and workarounds with plotting order can be ugly

示例代码:

%**** 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')

(移动代码块顺序以复制上述情况)

(move the code block order to replicate the situations mentioned above)

如何合理地解决此问题?

How to reasonably fix this?

推荐答案

如果您希望某个图形对象不产生图例(即使您再次打开或关闭图例也可以使用图例),则可以修改LegendInformation:

If you want a certain graphics object to not produce a legend (and that will work even if you toggle the legend off and on again), you can modify the 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对其进行循环:

If you try to turn off the legend on an array of handles, the best way is just to loop over them, with a try-wrapper for graphical objects that cannot produce a legend:

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

这篇关于隐藏图中某些图形对象的MATLAB图例条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆