![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/353231/image.gif)
How can I add colors on my 3D scatter plot ?
조회 수: 139 (최근 30일)
이전 댓글 표시
Hi,
I have three vectors (listeX, listeY, listeCycles). I want to plot using scatter3() function.
It results in this chart:
![cycles.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/260675/cycles.jpeg)
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');
댓글 수: 0
채택된 답변
Adam Danz
2020년 1월 8일
편집: Adam Danz
2020년 8월 28일
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()
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/353231/image.gif)
댓글 수: 5
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!