
Grouping Data for a 3d Scatter Plot
27 次查看(过去 30 天)
显示 更早的评论
I have my data in several groups and I want to do a 3d scatter plot where each group has a different colored marker to represent it, say its a dot. I would like group A1 to be red, group A2 to be blue group A3 to be purple etc.
What I do not want is to scale color by one of the factors which it seems to be very happy to do and is completely useless.
Unfortunately this seems to be the most difficult thing possible to do in Matlab. I don't want to learn how to code I just want to do it through the interface and it seems to be impossible, I've wasted an entire afternoon on this.
If someone can throw me a lifeline I'd really appreciate it.
0 个评论
回答(4 个)
Chad Greene
2015-6-24
Try this:
% Some data:
x = rand(30,1);
y = rand(30,1);
z = rand(30,1);
% Each data point is in one of 5 groups:
group = randi(5,30,1);
% What are those unique groups?
uniqueGroups = unique(group);
% RGB values of your favorite colors:
colors = brewermap(length(uniqueGroups),'Set1');
% Initialize some axes
view(3)
grid on
hold on
% Plot each group individually:
for k = 1:length(uniqueGroups)
% Get indices of this particular unique group:
ind = group==uniqueGroups(k);
% Plot only this group:
plot3(x(ind),y(ind),z(ind),'.','color',colors(k,:),'markersize',20);
end
legend('group 1','group 2','group 3','group 4','group 5')

The brewermap function is available on the File Exchange.
3 个评论
Alamgir M S M
2019-3-9
编辑:Walter Roberson
2019-3-9
Hi, that group is little different for me.
This is my Data-
[705.7142857 705.7142857 173.4285714 84.71428571 232.5714286 232.5714286 114.2857143 55.14285714 25.57142857 74.85714286 35.42857143 15.71428571 5.857142857 5.857142857]
I want to group same data into 1 group. This is 14*1 matrix where it has 3 pairs(705.7142857 705.7142857, 232.5714286 232.5714286 & 5.857142857 5.857142857) of same value data. So, I want these 3 pairs into 3 groups and rest of them into other 8 groups.
Could you help me to change this line from your code:
group = randi(11,14,1);
Thank you.
John Clarke
2015-6-24
1 个评论
Walter Roberson
2015-6-24
You need at least one line of code, just to start the scatter3 plot in the first place. Once the scatter3 plot exists you can do the rest through the interface.... but it is certainly less tedious if you also have a line of code to create the color table.
Walter Roberson
2015-6-24
It might depend upon which MATLAB version you are using, but at least up to R2014a, the graphical interface does not offer a method to start a scatter3 plot. It allows scatter plots to be started, but if you have a variable selected in the variable editor and have selected more than 2 columns, then the Plot tab greys out scatter3().
This might have been a limitation in R2014a; if you are using R2014b onwards perhaps it is possible.
If it was a 2D scatter plot you had been wanting to do, then it would have been possible using only the interface. Getting the colors right would be a nuisance using only the interface, but it would be possible.
Matters become much simpler if you were willing to type in at least one command.
0 个评论
John Clarke
2015-6-24
2 个评论
Walter Roberson
2015-6-24
The one line of code that is really needed is the one to create the scatter plot in the first case,
scatter3(x(:), y(:), z(:))
after that everything else can be done through the interface. But if you are going to type in the one command you can make things faster with
scatter3(x(:), y(:), z(:), 20, group(:))
provided that group contains small positive integers (max 128 on MS Windows before R2014b). After that it is just a matter of editing the colormap, which can be done through the interface. Most of us would rather write a small number of additional lines to set the colormap. For example,
scatter3(x(:), y(:), z(:), 20, group(:)); colormap(jet(5));
would do the whole coloring using 5 colors from the "jet" palette.
Christina Rigsby
2022-5-8
How do you add a legend labeling the 5 groups you've visualized with the suggested code:
scatter3(x(:), y(:), z(:), 20, group(:)); colormap(jet(5));
?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!