Borrar filtros
Borrar filtros

Changing color of plot

6 visualizaciones (últimos 30 días)
Thomas Wans
Thomas Wans el 24 de Oct. de 2021
Comentada: Rik el 27 de Oct. de 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 comentarios
Rik
Rik el 25 de Oct. de 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 el 27 de Oct. de 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

Iniciar sesión para comentar.

Respuestas (1)

Sargondjani
Sargondjani el 24 de Oct. de 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

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by