I have a typical scenario in which there is a Vector X and Vector Y. Vector X cont开发者_StackOverflowains increasing values, for example X = [1 1 1 2 2 3 4 4 4 4 4]. Vector Y contains real values of same size as X. Im looking to plot Index Vs Y with color change for each different value of X for the corresponding index.
For example, the plot should have color1 for the first 3 values of 1, color2 for the next 2 values of 2, color3 for 1 value 3 and so on.
Can any one help me
Building on Laurent's answer and implementing your "Index vs Y" requirement,
function color_plot(data_vector, color_vector)
styles={'ro','g.','bx','kd'};
hold off;
for i=unique(color_vector)
thisIdx=find(color_vector==i);
thisY=data_vector(color_vector==i);
thisStyle=styles{mod(i-1,numel(styles))+1};
plot(thisIdx,thisY,thisStyle);
hold on;
end
hold off;
My version also allows arbitrarily large color indices; if you don't have enough styles defined, it just wraps back around and reuses colors.
Update note I had to fix a sign above in the calculation oh thisStyle
.
Testing it with
X = [1 1 1 2 2 3 4 4 4 4 4];
Y=rand(size(X))
color_plot(Y,X)
now gives
A plot()
function option would be better (and maybe it exists).
Here's a workaround function to do this:
function colorPlot( data_vector, colors_vector)
%PLOTCOL plots data_vector with colors found in colors_vector
Styles=[{'r-'} {'g-'} {'b-'} {'k-'}];
last_off=0;
last_data=0;
for i=unique(colors_vector)
data_segment=data_vector(colors_vector==i);
len=length(data_segment);
if last_off==0
hold off;
plot( data_segment, 1:len,char(Styles(i)));
last_off=len;
else
plot([last_data data_segment],last_off:last_off+len,char(Styles(i)));
last_off=last_off+len;
end
last_data=data_segment(len);
hold on;
end
hold off;
end
Call it this way :
colorPlot(Y,X);
精彩评论