Effacer les filtres
Effacer les filtres

Changing color of plot

3 vues (au cours des 30 derniers jours)
Thomas Wans
Thomas Wans le 24 Oct 2021
Commenté : Rik le 27 Oct 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 commentaires
Rik
Rik le 25 Oct 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 le 27 Oct 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

Connectez-vous pour commenter.

Réponses (1)

Sargondjani
Sargondjani le 24 Oct 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

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by