필터 지우기
필터 지우기

Legend And Plot Disagree

조회 수: 1 (최근 30일)
Stashu Kozlowski
Stashu Kozlowski 2021년 2월 19일
댓글: Cris LaPierre 2021년 2월 20일
I am trying to plot three functions on the graph; horizontal, y=x, and a sawtooth function. When I plot all three of them, the legend sucsefully displays 3 different colours (one for each function), but the plot itself doesnt distingiuse between the 3 plots and colours two of them the same way. I have tried manuly setting the colours of them different but then the legend disagress agian. Anyone know how to fix this?
x = 0:0.1:16;
yClassical = 0*ones(length(x));
yVacuum = x;
yQuantum = 4.9/2*sawtooth(2*pi/4.9*x)+4.9/2;
hold on
plot(x, yVacuum, 'lineWidth', 4)
plot(x, yClassical, 'lineWidth', 4)
plot(x, yQuantum, 'lineWidth', 4)
legend('Vacuum', 'Classical Prediction', 'Quantum Prediction')
hold off

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 2월 20일
편집: Cris LaPierre 2021년 2월 20일
The problem is related to yClassical being a matrix instead of a vector. When using ones, if you only specify one input value, n, then the result is an nxn matrix. I think you want a 1xn.
Also, since you mulitiply it by zero anyway, I'll use the zeros function instead.
x = 0:0.1:16;
yClassical = zeros(1,length(x));
yVacuum = x;
yQuantum = 4.9/2*sawtooth(2*pi/4.9*x)+4.9/2;
plot(x, yVacuum, 'lineWidth', 4)
hold on
plot(x, yClassical, 'lineWidth', 4)
plot(x, yQuantum, 'lineWidth', 4)
legend('Vacuum', 'Classical Prediction', 'Quantum Prediction')
hold off
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2021년 2월 20일
If you are curious what is happening, when yClassical is a 161x161 matrix, MATLAB will treat each column as a series, meaning it will plot each column of Y as a separate line. Since Y is all zeros, it plots 161 lines one top of each other. The numbering just happens to work out that the last line (the one you see because it's plotted on top of the others), ends on the same color as Vacuum, giving the appearance of not having used a different color.
You can inspect this by opening the plot browser (figure toolbar > View > Plot browser).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by