Plot legend shows same color for 2 graphs

I plot a coordinate system with three different graphs (three different colors), but plot legend shows same color for two of them (B and C). This is what I did
figure(1)
plot(t,y3,'r',t,y2,'g',t,y1,'b')
leg=legend('A','B','C',2)
set(leg,'Interpreter','none')
I don't see what's wrong.

4 件のコメント

Geoff Hayes
Geoff Hayes 2016 年 2 月 4 日
Ljix - what are the dimensions of t, y3, y2, and y1? If one or more of these variables are not row or column arrays, then you may be plotting more than what you are expecting. See http://www.mathworks.com/matlabcentral/answers/146668-legend-and-graph-doesn-t-have-the-same-color for an idea of what may be going wrong.
Ljix
Ljix 2016 年 2 月 4 日
編集済み: Geoff Hayes 2016 年 2 月 4 日
This is how they are defined
N = 100;
t = linspace(0, 10, N);
for k1 = 1:length(t);
y3(:,k1) = CN*((t(k1)*eye(2*n)-A)\B);
end
Geoff Hayes
Geoff Hayes 2016 年 2 月 4 日
But what are the dimensions? How are y2 and y1 defined? Type the following in your command window
size(y3)
size(y2)
size(y1)
What do you observe?
Ljix
Ljix 2016 年 2 月 4 日
編集済み: Walter Roberson 2016 年 2 月 4 日
1 6000
2 6000
1 6000

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

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 2 月 4 日

1 投票

Your y2 is 2 x 6000. When you plot t, y2 MATLAB is going to notice that your t is 6000 and will figure that needs to be matched to the 6000 of your y2, so MATLAB will automatically transpose your y2 to be 6000 x 2 . Then it will draw 2 lines for y2, one for each column. Those two lines are throwing off your legend. You are not drawing 3 different graphs, you are drawing 4 different graphs.

6 件のコメント

Ljix
Ljix 2016 年 2 月 4 日
I don't understand :( I just wanted to plot graphs of three different functions with same variable
Walter Roberson
Walter Roberson 2016 年 2 月 4 日
You do not have three different functions, you have four different functions. Your y2 is defining two different functions in the same variable.
As an example:
t = 1:10;
y = [t.^2;t.^3];
plot(t,y)
This has one variable, y, but stores two different functions in that variable, and the plot produces two different lines.
Ljix
Ljix 2016 年 2 月 4 日
But y2 is defined with
y2(:,k1) = Cn*((t(k1)*eye(size(An))-An)\Bn);
Only difference from y3 and y1 are different matrices in definition of a function. I don't see how is then y2 of a different size? Any idea?
Walter Roberson
Walter Roberson 2016 年 2 月 4 日
Your An or Bn might be different sizes than the corresponding matrices you use for y1 or y2.
Ljix
Ljix 2016 年 2 月 4 日
Yes, they are different size but right size is always a scalar.
Walter Roberson
Walter Roberson 2016 年 2 月 4 日
"Solution, returned as a vector, full matrix, or sparse matrix. If A is an m-by-n matrix and B is an m-by-p matrix, then x is an n-by-p matrix, including the case when p==1."
So if your A was something by 2, then you would get a 2 x 1 result instead of a scalar

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

Kelly Kearney
Kelly Kearney 2016 年 2 月 4 日

0 投票

What are the dimensions of your variables? If any are arrays rather than vectors, they will create multiple lines. In that case, you'll need to save the line handles and pass only the first of each color to legend:
t = 1:10;
y1 = rand(10,1); % 1 line
y2 = rand(10,2); % 2 lines
y3 = rand(10,1); % 1 line
figure(1)
h = plot(t,y3,'r',t,y2,'g',t,y1,'b');
leg = legend(h([1 2 4]), 'A','B','C');

1 件のコメント

Ljix
Ljix 2016 年 2 月 4 日
This is how t and y3 (y2 and y1 same with different matrix) are defined
N = 100;
t = linspace(0, 10, N);
for k1 = 1:length(t);
y3(:,k1) = CN*((t(k1)*eye(2*n)-A)\B);
end

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

カテゴリ

タグ

質問済み:

2016 年 2 月 4 日

コメント済み:

2016 年 2 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by