Use scatter3d to plot large dataset with 7 colors

I have a dataset of about 1500 observations with three numerical variables X, Y, Z, plus a variable C assigning one of seven colors to each observation. How do I use scatter3d to get the corresponding graph (where each observation gets the color as specified in C)? I find many examples/posts for scatter3d on mathworks, but they all involve just a slightly different problem.

 Accepted Answer

It may be necessary to describe the problem in greater detail. The scatter3 documentation implies that it would be appropriate for the problem.
If ‘X’, ‘Y’, ‘Z’ and ‘C’ are matrices, it would be necessary to convert them to vectors to use scatter3:
figure
scatter3(X(:), Y(:), Z(:), [], C(:))
I sometimes find it preferable to use stem3, since it establishes the locations of the data, then hold, then scatter3 to add the colours.

8 Comments

Thanks. I don't think they are matrices as I imported the data right from an Excel file. I have three columns with numbers in (-1,1) and a column with numbers in {1, ..., 7}, for the color of each observation.
If they are all the same lengths, then just use scatter3 (or stem3 then scatter3) to plot them. That should be straightforward.
Thanks. What indeed ultimately worked was
Data = xlsread('data.xls')
x=Data(:,1);
y=Data(:,2);
z=Data(:,3);
c=Data(:,4);
scatter3(x,y,z,[],w)
My pleasure!
HJowever, I do not see that ‘w’ has been defined anywhere.
If my Answer helped you solve your problem, please Accept it!
.
That's a typo. It should read:
Data = xlsread('data.xls')
x=Data(:,1);
y=Data(:,2);
z=Data(:,3);
c=Data(:,4);
scatter3(x,y,z,[],c)
I assumed so. I wanted to be certain.
As always, my pleasure!

Sign in to comment.

More Answers (1)

I suggest to use surf or mesh instead of scatter 3d and colorbar for setting seven colors.
surf(X,Y,Z);
h=colorbar('Ticks',C);

3 Comments

Thanks. Perhaps the issue may be a bit more basic as when I try surf or mesh I learn that my Z variable must be a matrix not a scalar or vector. I imported the data right from an Excel file. So I have three columns with numbers in (-1,1) and one column with numbers if {1, ..., 7}, for the color coding of each observation.
I am not sure if it can be applied to your problem, but I have used the following before using surf
[X,Y,Z] = meshgrid(x,y,z)
Might the data be too large for this? It doesn't halt.

Sign in to comment.

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!