Displaying two different variable legends in the same figure

I am trying to display two legends in the same figure.Here, z1,z2 and z3 all are variables.Please suggest.
Suppose z1=0.4, z2=0.9, & z3=0.25
lgd1=legend(['r_th=' num2str(z1)]);
lgd2=legend(['K=' num2str(z2+z3)]);

3 件のコメント

jonas
jonas 2018 年 5 月 3 日
Do you need multiple legends or can you instead use one legend with multiple rows?
Dharma
Dharma 2018 年 5 月 3 日
Thanks. I am trying to get display like this:
r_th=0.4, K=0.9+0.25=1.15.
It can be in same row or multiple rows.
jonas
jonas 2018 年 5 月 3 日
You still did not answer my question though. Yuvaraj's answer shows you how to manipulate the plot by superimposing two axes with one legend on each axes, which is a way to make 2 legends. I believe what you are really after is a single legend with multiple inputs, which is really basic. See the documentation for the correct legend syntax.

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

回答 (2 件)

Yuvaraj Venkataswamy
Yuvaraj Venkataswamy 2018 年 5 月 3 日

0 投票

hAx(1) = axes();
hLine(1) = plot(1:10, 'Parent',hAx(1));
set(hAx(1), 'Box','off')
legend(hLine(1), 'line')
hAx(2) = copyobj(hAx(1),gcf);
delete( get(hAx(2),'Children') ) hLine(2) = plot(sin(1:10), 'Color','r', 'Parent',hAx(2));
set(hAx(2), 'Color','none', 'XTick',[], ... 'YAxisLocation','right', 'Box','off') %# make it transparent legend(hLine(2), {'curve'}, 'Location','NorthWest', 'Color','w')
Star Strider
Star Strider 2018 年 5 月 3 日

0 投票

You can only plot one legend in each axes object.
With that restriction, you can plot them as 1 row and 2 columns, (or 2 columns and 1 row in R2018a and later):
legend({sprintf('r_{th} = %.2f',z1), sprintf('K = %.2f + %.2f = %.2f', z2, z3, z2+z3)}, 'Location','N') % One Column, Two Rows
legend({sprintf('r_{th} = %.2f',z1), sprintf('K = %.2f + %.2f = %.2f', z2, z3, z2+z3)}, 'Location','N', 'NumColumns',2) % Two Columns, One Row (R2018a+)

2 件のコメント

Dharma
Dharma 2018 年 5 月 4 日
Both r_th and K are initial parameters (not axes parameter) and I
define their value at the beginning of code. For example:
z1=0.4; z2=0.9; z3=0.2;
r_th=z1;
K=z2+z3;
What I am trying to do is I want to choose their different value
randomly (at the beginning of code) without changing their value also in
legend every time. Please suggest.
Star Strider
Star Strider 2018 年 5 月 4 日
I have nothing to suggest. I have no idea what you want.

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

カテゴリ

質問済み:

2018 年 5 月 3 日

コメント済み:

2018 年 5 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by