How do I vary the color of a 3D scatter plot by it's location in the graph?

2 views (last 30 days)
I'm trying to figure out how to replicate in MATLAB what another software does automatically(if I could find a way to attach a picture, I would). Essentially, I have a 3D scatter plot of about 2000 points, and I want the color of the points to vary based on on their position in the z-axis. I think there's a way to do it with colormap, but I haven't been able to figure it out. I also tried creating a series of if statements to create a vector C that would be equal in length to my x, y and z vectors and control the color, but I keep getting the following error message, and twice when I tried running the program like this, MATLAB has completely crashed on me:
Error using scatter3 (line 75) S must be a scalar or a vector the same length as X.
Error in Displacement (line 33) scatter3 (dis, DS{3}.y, DS{3}.x, C, S, 'fill')
Here's my code:
dis = DS{ii}.r3
l = size(dis)
S = 10
C = [ ]
for i = 1:l(1)
x = DS{ii}.r3(i)
if -2e-10<x<2e-10
C = [C; [0 1 0]]; %green
elseif 2e-10<x<5e-10
C = [C; [1 1 0]]; %yellow
elseif 5e-10<x<7.5e-10
C = [C; [1 0 0]]; %red
elseif x>7.5e-10
C = [C; [1 0 1];]; %magenta
elseif -5e-10<x<-2e-10
C = [C; [0 1 1]]; % cyan
elseif -7.5e-10<x<-5e-10
C = [C; [0 0 1]]; %blue
elseif x<-7.5e-10
C = [C; [0 0 0]]; %black
else
C = [C; [0 1 0]]; %green
end
end
scatter3 (dis, DS{3}.y, DS{3}.x, C, S, 'fill')
xlabel('Displacement')
ylabel('x axis')
zlabel('y axis')
title(DS{ii}.d1)
Thanks!
JB

Answers (0)

Community Treasure Hunt

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

Start Hunting!