How to avoid unnecessary legends in the graph?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
OMAR MOUSA
am 7 Okt. 2023
Beantwortet: Voss
am 7 Okt. 2023
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
0 Kommentare
Akzeptierte Antwort
Bruno Luong
am 7 Okt. 2023
Bearbeitet: Bruno Luong
am 7 Okt. 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 Kommentare
Dyuman Joshi
am 7 Okt. 2023
Bearbeitet: Dyuman Joshi
am 7 Okt. 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')
Weitere Antworten (2)
dpb
am 7 Okt. 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 Kommentar
Bruno Luong
am 7 Okt. 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. :-)
Voss
am 7 Okt. 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!