Changing color of plot

조회 수: 10 (최근 30일)
Thomas Wans
Thomas Wans 2021년 10월 24일
댓글: Rik 2021년 10월 27일
Hi,
I need to make plot that has two colors of line red and blue, and the line should be red when value is increasing and blue when value is decreasing and when I use dotted line it works 'b*' but without this it doesn't work 'b' I think I should define first point of plot but I'm not sure.
x = -10:.01:10;
for ii = 1:length(x)
y(ii) = sin(x(ii)); % Data point ii has come in.
if y(ii)<0
c = 'b*';
else
c = 'r*';
end
plot(x(ii),y(ii),c)
hold on
end
  댓글 수: 2
Rik
Rik 2021년 10월 25일
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
Rik
Rik 2021년 10월 27일
You don't get it, do you? I'm probably more stubborn than you. I will keep restoring your edit, so you might as well give up now.
Changing color of plot when value is increasing
Hi,
I need to make plot that has two colors of line red and blue, and the line should be red when value is increasing and blue when value is decreasing and when I use dotted line it works 'b*' but without this it doesn't work 'b' I think I should define first point of plot but I'm not sure.
x = -10:.01:10;
for ii = 1:length(x)
y(ii) = sin(x(ii)); % Data point ii has come in.
if y(ii)<0
c = 'b*';
else
c = 'r*';
end
plot(x(ii),y(ii),c)
hold on
end

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

답변 (1개)

Sargondjani
Sargondjani 2021년 10월 24일
The problem is that you plot only 1 point at a time. So there is no line to plot, just a point. That's why your code doesn't work when you only specify a line type, for example c = '-b';
You could plot line segments:
plot([x(ii-1),x(ii)],[y(ii-1),y(ii)],c)
Another thing: you mention that you want it to be red, when value is increasing, so you criterion should be y(ii)>y(ii-1).
So your code becomes:
x = -10:.01:10;
y = sin(x);
for ii = 2:length(x)
if y(ii)>y(ii-1)
c = '-b';
else
c = '-r';
end
plot([x(ii-1),x(ii)],[y(ii-1),y(ii)],c)
hold on
end

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by