필터 지우기
필터 지우기

Legend of graph doesn't match plot color and style

조회 수: 2 (최근 30일)
Kevin Navarro
Kevin Navarro 2019년 1월 20일
댓글: Star Strider 2019년 1월 20일
Hello,
I have been looking for a way to match my legend color and characters to what is plotted on the graph. For some reason after the first two legend values, the legend ignores the color and icon that has been plotted and just recycles the second red dotted value for the rest of the plots. How would I get the legend to match the plot?
figure(2)
H=4;
t=0;
omega=2*pi/10;
omegat=omega*t;
kx=-2*pi:.1:2*pi;
eta=H/2*cos((kx)+(omegat));
etaH=eta/H;
plot(kx,etaH)
xlabel('Dimentionless Distance kx')
ylabel('Dimentionless Elevation \eta/H')
% t=0
t=0;
omegat0=omega*t;
hold on
plot(kx,omegat0,'r.')
% t=1
hold on
t=1;
omegat1=omega*t;
plot(kx,omegat1,'y.')
% t=2
hold on
t=2;
omegat2=omega*t;
plot(kx,omegat2,'g.')
% t=3
hold on
t=3;
omegat3=omega*t;
plot(kx,omegat3,'m.')
% Legend
legend('\eta/H as a function of kx', '\omegat at t=0','\omegat at t=1','\omegat at t=2','\omegat at t=3')

채택된 답변

Star Strider
Star Strider 2019년 1월 20일
You can plot an independent variable aganst a dependentent variable constant, however you will end up with the problem you are seeing. Multiplying the constant by a ones vector solves the problem:
hold all
plot(kx,omegat0*ones(size(kx)),'r.')
% t=1
t=1;
omegat1=omega*t;
plot(kx,omegat1*ones(size(kx)),'y.')
% t=2
t=2;
omegat2=omega*t;
plot(kx,omegat2*ones(size(kx)),'g.')
% t=3
t=3;
omegat3=omega*t;
plot(kx,omegat3*ones(size(kx)),'m.')
hold off
  댓글 수: 2
Kevin Navarro
Kevin Navarro 2019년 1월 20일
Worked like a charm, thank you!
Star Strider
Star Strider 2019년 1월 20일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by