How can I add colors on my 3D scatter plot ?

117 views (last 30 days)
Hi,
I have three vectors (listeX, listeY, listeCycles). I want to plot using scatter3() function.
It results in this chart:
cycles.JPG
I would like to color in different colors the points on the vertical axis Z, according to their Z-coordinate.
For instance, having red for the bottom data, green for medium and blue for the data above.
Here is my code . Thank you very much !
figure
scatter3(listeX,listeY,listeCycles,'MarkerEdgeColor','k','MarkerFaceColor',[0 .75 .75])
xlabel('Temps ouverture minimum (s)');
ylabel('deadband (°)');
zlabel('Nombre de cycles sur projection 10 000s');

Accepted Answer

Adam Danz
Adam Danz on 8 Jan 2020
Edited: Adam Danz on 28 Aug 2020
Specify color from scatter3()
You can specify the color of each point when you call scatter3(X,Y,Z,S,C) or by using the property-value, scatter3(X,Y,Z,. . .,'MarkerFaceColor',C) where C is an n-by-3 matrix of RGB value, one row for each point.
Specify color using the scatter object handle
Alternatively, you can specify the color after plotting by setting the CData property as an n-by-3 matrix of RGB values.
h = scatter3(. . ., 'filled');
h.CData = C;
Creating the RGB matrix
One simple way to create the RGB color matrix is to use one of Matlab's colormaps and specify the number of points. This example uses jet and inputs the number of values in the first input to scatter3, X. See this list for other built-in colormaps.
C = jet(numel(X));
To add a colobar,
set(gca, 'colormap', C)
colorbar()
Full Demo
figure
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
h = scatter3(x,y,z,'filled','MarkerEdgeColor','k');
axis equal
box on
C = jet(numel(x));
h.CData = C
set(gca, 'colormap', C)
colorbar()
  5 Comments
Dipali Ghodake
Dipali Ghodake on 28 Aug 2020
How to show colormap to this plot?

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!