How to avoid unnecessary legends in the graph?

3 次查看(过去 30 天)
Hi all, I wan to avoid or delete the unwanted legends in the graph is there anyway to do that?
Like this graph I didn't put the names of data1, data2, ..., data5 but still appears in the graph. and this is the code that i worte without mention the unwanted legends!!
plot(t,TGN,t,T1N,t,ML1N,t,ML2N,t,ACN,t,EMSB1N,t,MFN,t,mainN);
xticks([43200 129600 216000 302400 388800 475200])
xticklabels({'Monday','Tuesday','Wendsday','Thursday','Friday', 'Saturday'})
xlim([0-1000 518400+1000])
ylabel('Current (A)')
title('Neutral line')
legend({'DB TG','DB T1','DB ML1','DB ML2','CP AC','EMSB 1','SSB MF','Aggregate'},'NumColumns',8)
x=[86400 172800 259200 345600 432000];
xline(x,'--k')
grid, grid minor

采纳的回答

Bruno Luong
Bruno Luong 2023-10-7
编辑:Bruno Luong 2023-10-7
h=plot(t,TGN,t,T1N,t,ML1N,t,ML2N,t,ACN,t,EMSB1N,t,MFN,t,mainN);
xticks([43200 129600 216000 302400 388800 475200])
xticklabels({'Monday','Tuesday','Wendsday','Thursday','Friday', 'Saturday'})
xlim([0-1000 518400+1000])
ylabel('Current (A)')
title('Neutral line')
x=[86400 172800 259200 345600 432000];
xline(x,'--k')
legend(h,{'DB TG','DB T1','DB ML1','DB ML2','CP AC','EMSB 1','SSB MF','Aggregate'},'NumColumns',8)
grid, grid minor
  2 个评论
OMAR MOUSA
OMAR MOUSA 2023-10-7
Thank you very much it works. I took hours to see what is the problem and you just took seconds to check it :)
Dyuman Joshi
Dyuman Joshi 2023-10-7
编辑:Dyuman Joshi 2023-10-7
You can also avoid this by turning the auto-update feature off.
The names data1 to data5 correspond to the xlines you have drawn (as can be observed from the figure). When you add or delete a data-series from the axes after you have define a legend, it gets automatically updated.
Setting the AutoUpdate property to off will stop modifying legend automatically.
legend({'DB TG','DB T1','DB ML1','DB ML2','CP AC','EMSB 1','SSB MF','Aggregate'},...
'NumColumns',8,'AutoUpdate','off')

请先登录,再进行评论。

更多回答(2 个)

dpb
dpb 2023-10-7
Yet another solution (and the one I think most appropriate, of course!)....
plot(t,TGN,t,T1N,t,ML1N,t,ML2N,t,ACN,t,EMSB1N,t,MFN,t,mainN);
xticks([43200 129600 216000 302400 388800 475200])
xticklabels({'Monday','Tuesday','Wendsday','Thursday','Friday', 'Saturday'})
xlim([0-1000 518400+1000])
ylabel('Current (A)')
title('Neutral line')
legend({'DB TG','DB T1','DB ML1','DB ML2','CP AC','EMSB 1','SSB MF','Aggregate'},'NumColumns',8)
x=[86400 172800 259200 345600 432000];
xline(x,'--k','Annotation','off')
grid, grid minor
Tell it you do not want to put the default constant line labels in the legend when you create them...
@Bruno Luong's solution relies on not calling legend until the end--in this case that's simple enough to rearrange the code; in other cases that might be less convenient.
@Dyuman Joshi's solution modifies the behavior of legend to not add any additional labels regardless--also in this case that would not appear to be a bad thing but again, in some instances, it's possible some more "real" data will be added later that should appear in the legend automagically.
The above just controls the specific division lines property alone and doesn't rely on position in code sequence nor change the other behavior of legend.
  1 个评论
Bruno Luong
Bruno Luong 2023-10-7
I like your solution.
However it is a good habit to specify handle graphic whenever we plot something. Never rely on the default object(s). Control everything and everyone. :-)

请先登录,再进行评论。


Voss
Voss 2023-10-7
Set the 'HandleVisibility' of the ConstantLines created by xline() to 'off'.
figure('Position',[10 10 1200 500]);
plot(1:519400,(1:519400).'/519400*14+(0:7));
xticks([43200 129600 216000 302400 388800 475200])
xticklabels({'Monday','Tuesday','Wendsday','Thursday','Friday', 'Saturday'})
xlim([0-1000 518400+1000])
ylabel('Current (A)')
title('Neutral line')
legend({'DB TG','DB T1','DB ML1','DB ML2','CP AC','EMSB 1','SSB MF','Aggregate'},'NumColumns',8)
x=[86400 172800 259200 345600 432000];
xline(x,'--k','HandleVisibility','off')
grid, grid minor

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by