Hossein Dehghani
Hossein Dehghani

Reputation: 93

How to assign color to points in graph

I need to plot a graph in which each point (x,y,z) has a color assigned based on its value. How can I do this in Matlab? I have tried: scatter3(x, y, z, c) but I had difficulty with color.

Consider this example: (x_i,y_i,z_i) has value of v_i. I want the point with max value to be red and the point with min value to be blue.

Upvotes: 1

Views: 109

Answers (2)

Rijul Sudhir
Rijul Sudhir

Reputation: 2132

Use this code. I am assuming that you need the color on the basis of z. That is why there is a second z in scatter3. You can replace it with another matrix on the basis of which color will be displayed.

s=50; %size of marker
scatter3(x, y, z, s ,z,'filled');
colormap(jet);
colorbar;

If you need a color variation blue-white-red . Then use this code.

s=50; %size of marker
scatter3(x, y, z, s ,z,'filled');
m = size(get(gcf,'colormap'),1)/2;
steps = (0:m-1)'/max(m-1,1);
cm_red_blue = [ steps steps ones(m,1);ones(m,1) flipud(steps) flipud(steps) ]; 
colormap(cm_red_blue);
colorbar;

Upvotes: 1

shamalaia
shamalaia

Reputation: 2351

you have to use it like:

scatter3(x, y, z, s, c)

where s is the size of the markers.

If you use it like you did Matlab thinks that the color is a size value and most probably returns an error.

Upvotes: 1

Related Questions