plot different colors in one trace concerning to the values

Hey everybody, I have an array with values and the appropriated plot (you can see among). I want that the pixels of the values 1-500 are colored in red, 501-550 in blue and 551-2000 in green. To cut a long story short: Is it possible to use different colors for one trace concerning on the values?
Thanks for helping, regards tim

Réponses (1)

Mischa Kim
Mischa Kim le 18 Fév 2014
Modifié(e) : Mischa Kim le 18 Fév 2014
Hello Tim, would this do?
x = linspace(1,2000,2000);
y = sin(x/100);
xx = {x(x<501) x(x>=501 & x<550) x(x>=550)};
set(gcf,'DefaultAxesColorOrder',[1 0 0;0 0 1;0 1 0])
hold all
for ii = 1:3
plot(xx{ii},y(xx{ii}))
end
With the set command you can change the color ordering.

3 commentaires

This is tricky code as xx might not be valid indices into y … An improvement:
x = linspace(1,2000,1999) ; % this may cause problems in Mischa's code
y = sin(x/100);
tf = {x<501 (x>=501 & x<550) x>=550}; % changed!
set(gcf,'DefaultAxesColorOrder',[0 1 0;0 0 1;1 0 0])
hold on
for ii = 1:3
plot(xx(tf{ii}),y(tf{ii}))
end
hold off
Also note that there will be breaks between the three sections ...
Hey Mischa and Jos, thanks for your answer. One big problem is that I don't have an function to describe course of the values. I just have the values.
That's not a problem. You could also do
y = rand(2000,1); % your data
x = 1:length(y);
With x you simply count data points. In other words, you use x to define the intervals with the different color coding.

Connectez-vous pour commenter.

Question posée :

le 18 Fév 2014

Commenté :

le 18 Fév 2014

Community Treasure Hunt

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

Start Hunting!

Translated by