简体   繁体   English

如何从现有的图例中删除条目?

[英]How to remove entries from an existing figure legend?

I have a Matlab figure, let's say it's MyFigure.fig. 我有一个Matlab人物,比如说MyFigure.fig。 This figure has 12 curves with its respective legend entries. 该图有12条曲线及其相应的图例条目。 However, I want to delete one of the curves and its legend. 但是,我要删除其中一条曲线及其图例。 The curve was easy to delete (just selecting it with the cursor and pressing delete), but when I try to delete its legend, I can only delete the name. 曲线很容易删除(只需使用光标选择它并按Delete键即可),但是当我尝试删除其图例时,只能删除名称。

I have been looking for this and I have found other ways to do it. 我一直在寻找这个方法,并且已经找到其他方法来做到这一点。 I have found helpful the information of the next link 我发现下一个链接的信息很有帮助

http://es.mathworks.com/help/matlab/creating_plots/controlling-legends.html http://es.mathworks.com/help/matlab/creating_plots/controlling-legends.html

but, I cannot carry it out. 但是,我无法执行。 The main problem I found here is this: 我在这里发现的主要问题是:

  • If I try to do it like this, I have no problems: 如果我尝试这样做,我将没有问题:

     Example (not my case): x=0:0.01:pi; y=sin(x); H=plot(x,y); hAnnotation = get(H, 'Annotation'); 
  • But, in my case, I don't have to use "plot" because I have already the figure. 但是,就我而言,我不必使用“图”,因为我已经有了该图。 So, when I open it and I try to get its handle (by H=gcf or H=gca ) and then, I write the code 因此,当我打开它并尝试获取它的句柄时(通过H = gcfH = gca ),然后编写代码

     hAnnotation = get(H, 'Annotation'); 

I have the next errors: 我有下一个错误:

  1. The name 'Annotation' is not an accessible property for an instance of class 'figure' (in the case of gcf) 名称“ Annotation”不是类“ figure”的实例的可访问属性 (对于gcf)
  2. The name 'Annotation' is not an accessible property for an instance of class 'axes' (in the case of gca) 名称“ Annotation”不是类“ axes”的实例的可访问属性(对于gca)

Well, there are several points in your code, that are going wrong. 好吧,您的代码中有几处地方出了问题。 Coming straight to your problem, that's what I suggest: 直接解决您的问题,这就是我的建议:

As far as I can see, this is the same issue that is asked and answered here , BUT the other way around: instead of adding information, you want to remove entries of the legend. 据我所知,这是在此处提出和回答的同一问题,但反过来说:您不想删除信息,而是要删除图例的条目。 In fact, that is leading to the exact same thing: you want to update the legend. 实际上,这导致了完全相同的事情:您想更新图例。 Thus you can use the same method as described in the mentioned question: 因此,您可以使用与上述问题相同的方法:

If you plot something, use the 'DisplayName'-parameter. 如果要绘制某些内容,请使用“ DisplayName”参数。 In your example, that would be: 在您的示例中,将是:

plot(x,y, 'DisplayName', 'myNameForThisLineGoesHere');
%now, after you added OR removed a line (etc.), you have to update the legend:
legend('off'); legend('on')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM