Changing color of plot

3 次查看(过去 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 中查找有关 Data Distribution Plots 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by