How to avoid unnecessary legends in the graph?

3 views (last 30 days)
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

Accepted Answer

Bruno Luong
Bruno Luong on 7 Oct 2023
Edited: Bruno Luong on 7 Oct 2023
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 Comments
OMAR MOUSA
OMAR MOUSA on 7 Oct 2023
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 on 7 Oct 2023
Edited: Dyuman Joshi on 7 Oct 2023
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')

Sign in to comment.

More Answers (2)

dpb
dpb on 7 Oct 2023
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 Comment
Bruno Luong
Bruno Luong on 7 Oct 2023
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. :-)

Sign in to comment.


Voss
Voss on 7 Oct 2023
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

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!