'legend' problem

49 ビュー (過去 30 日間)
Silvia
Silvia 2012 年 1 月 5 日
コメント済み: Tomas 2022 年 11 月 11 日
Hello,
I'm using hold on to draw different vectors on the same graph but every time I add a curve I need to add its legend. My problem is that when I write 'legend('Curve Name')' it appears only the last and I'd need to see all the names together$ on the same legend.
Thank you very much
Silvia
  1 件のコメント
Silvia
Silvia 2012 年 1 月 5 日
I know I can write: legend('Curve name1','Curve name2','Curve name3'...). But i'd need do it separately.
I'm doing:
legend('Curve name1')
legend('Curve name2')
legend('Curve name3')
And after this it appears only the third one.

サインインしてコメントする。

採用された回答

Junaid
Junaid 2012 年 1 月 5 日
Dear Silva,
The best way is that when you plot give the name to the curve instantly. let say
plot([1:10],'Color','r','DisplayName','Vector 1');hold on;
plot as many vectors you want. Just in end type this.
legend('show');
  3 件のコメント
Silvia
Silvia 2012 年 1 月 9 日
Thank you very much :).My problem is solved. Now I'm trying to put different markers each time
Tomas
Tomas 2022 年 11 月 11 日
Thank you!! Used to troubleshoot to find where to leave ' ' in my text to leave things blank
e.g. : legend('','','roots','Location','south');

サインインしてコメントする。

その他の回答 (2 件)

Matt Tearle
Matt Tearle 2012 年 1 月 5 日
As Junaid says, plotting all together is the nicest solution. However, if, for some reason, that isn't feasible for you, you can do this to recover the strings used so far and add a new one:
h = legend('Curve name 1');
for k = 1:n
% add plot
h = legend([get(h,'String'),'Curve name k']);
end
  1 件のコメント
Silvia
Silvia 2012 年 1 月 9 日
Thank you :)

サインインしてコメントする。


Patrick Kalita
Patrick Kalita 2012 年 1 月 5 日
This is along the same lines as Junaid's answer. The idea is to use the plot's DisplayName property to set the legend text for each curve. A combination of legend off and legend show will cause the legend to be repopulated with any new plots that have been added. For example:
hold all
for i = 1 : 10
plot( rand(1, 50), 'DisplayName', sprintf( 'Plot #%d', i ) );
legend off; legend show;
pause(0.5);
end
hold off
  1 件のコメント
Silvia
Silvia 2012 年 1 月 9 日
Thank you :)

サインインしてコメントする。

カテゴリ

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