How to avoid unnecessary legends in the graph?

14 ビュー (過去 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 Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by