Filter löschen
Filter löschen

Changing color of plot

2 Ansichten (letzte 30 Tage)
Thomas Wans
Thomas Wans am 24 Okt. 2021
Kommentiert: Rik am 27 Okt. 2021
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 Kommentare
Rik
Rik am 25 Okt. 2021
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 am 27 Okt. 2021
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sargondjani
Sargondjani am 24 Okt. 2021
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

Kategorien

Mehr zu Graphics finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by