mallow
mallow

Reputation: 83

pcolor in scatter plot matlab

I have a large matrix DAT(50000+,42). I am plotting 2 rows of this matrix on the x and y axes, and want the plot points to vary in color due to the value of a separate row. Can anybody advise? pcolor will not work for me due to "color data input must be a matrix" error. TIA

X = DAT(:,21);

Y = DAT(:,22);

Z = DAT(:,28);

plot(X,Y,'*');

hold on

pcolor(X,Y,Z);

hold off

Upvotes: 1

Views: 9864

Answers (2)

Michiel
Michiel

Reputation: 189

You can select the colors from an array generated with colormap like this:

DAT = randn(30,42);
X = DAT(:,21);
Y = DAT(:,22);
Z = DAT(:,28);

[dummy ID]=sort(Z);
colors=colormap(jet(length(Z)));

figure
for i=1:length(Z)
plot(X(i),Y(i),'*','Color',colors(ID(i),:));
hold on
end

The only issue with this technique is that you cannot do plots with millions of points because of the looped plotting, but otherwise it works like a charm:

enter image description here

Upvotes: 1

Tobias
Tobias

Reputation: 4074

You could consider using scatter()

% random sample data
DAT = randn(30,42);
X = DAT(:,21);
Y = DAT(:,22);
Z = DAT(:,28);

scatter(X,Y,50,Z); % x,y,size,color -> size can also be a vector
% scatter(X,Y,50,Z,'*'); % to also change the marker type

enter image description here

Upvotes: 2

Related Questions