Remove Objects from Legend When You Have Also Used

2019-08-01 07:37发布

I've plotted data points and fitted an exponential curve between them using 'fit' in Matlab. The problem is that with the fit-function I got the fitted line I wanted to plot, but also extra markers on top of my regular markers. I solved this problem by plotting the wanted markers on top of the unwanted so that they couldn't be seen. Now to the problem. When I want to show the legends those dots are also in there. How can I remove the markers from the legend without removing the fitted line since both of them are hidden inside the fit-function? Can I stop 'fit' from plotting the unwanted markers? So, I want to remove the blue dot called 'hoff' in the picture below.

enter image description here

1条回答
我只想做你的唯一
2楼-- · 2019-08-01 08:37

You can leave out legend-entries by manually leaving out the handles of the lines, that you dont want to be in the legend. Try this:

%if you plot something, that you want showing up in the legend, save its handle:
h_line = plot(x,y)
%you dont want to show up this line? dont do anything, just plot it:
plot(myMarker)
%then set the legend-> you can add the text for your legend-entries with 
%a cell-array   containing the strings you want to show up:
legend([h_line another_line],{'Text1' 'Text2'})

with the example (see comments) I came to this solution:

close all
X=[1:10];
Y=X*0.5+0.1;
ft = fittype('poly2'); 
f = fit(X', Y',ft); 
ha=plot(f)
hold on
hc=plot(X,Y)
hb=errorbar(X, Y, X*0.1, 'squarek','MarkerFaceColor','k','markersize',5)
hleg1 = legend([ha hc],{'hnh','ghg'});

-> this is just about splitting the plot-command. Hope that helps...

the result should look like this:

enter image description here

查看更多
登录 后发表回答