How to avoid unnecessary legends in the graph?

조회 수: 3 (최근 30일)
OMAR MOUSA
OMAR MOUSA 2023년 10월 7일
답변: Voss 2023년 10월 7일
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