81 views (last 30 days)
Show older comments
John Clarke on 24 Jun 2015
-
-
Link
Direct link to this question
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot
Commented: Christina Rigsby on 8 May 2022
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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (4)
Chad Greene on 24 Jun 2015
Open in MATLAB Online
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 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Walter Roberson on 24 Jun 2015
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot#comment_294334
Open in MATLAB Online
It can be done without the loop.
x = rand(30,1);
y = rand(30,1);
z = rand(30,1);
group = randi(5,30,1);
[uniqueGroups, uga, ugc] = unique(group);
colors = brewermap(length(uniqueGroups),'Set1'); %or any other way of creating the colormap
markersize = 20; %change to suit taste
scatter3(x(:), y(:), z(:), markersize, colors(ugc,:));
And in the case where the groups are known to be small positive integers (as was the case in your example)
x = rand(30,1);
y = rand(30,1);
z = rand(30,1);
group = randi(5,30,1);
colors = brewermap(max(group),'Set1'); %or any other way of creating the colormap
markersize = 20; %change to suit taste
scatter3(x(:), y(:), z(:), markersize, colors(group,:));
Basically once you already have the data, the only complication is creating the color table with the desirable set of colors for each group. Once that is done, a single call creates the plot and colors each marker appropriately.
When it is required that everything be done through the interface, the single largest problem, at least in R2014a, is that scatter3() is not a plot that can be chosen from the interface once you have your columns selected. If it were available then after that the interface could be used to get into the scatter plot and edit the CData values to suit -- a process made much easier if you have the color table already stored.
Alamgir M S M on 9 Mar 2019
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot#comment_679116
Edited: Walter Roberson on 9 Mar 2019
Open in MATLAB Online
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.
Walter Roberson on 9 Mar 2019
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot#comment_679260
Answered at https://www.mathworks.com/matlabcentral/answers/449110-hi-i-want-to-group-my-data-into-11-groups#answer_364549
Sign in to comment.
John Clarke on 24 Jun 2015
-
-
Link
See AlsoHow to Plot 3D in MATLABDirect link to this answer
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot#answer_183897
so there's no way to do it through the interface? I have to write code?
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Walter Roberson on 24 Jun 2015
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot#comment_294336
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.
Sign in to comment.
Walter Roberson on 24 Jun 2015
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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
John Clarke on 24 Jun 2015
OK well thanks for the help all maybe I'll tackle the code writing after a good nights sleep when I'm in a better frame of mind.
2 Comments Show NoneHide None
Show NoneHide None
Walter Roberson on 24 Jun 2015
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot#comment_294352
Open in MATLAB Online
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 on 8 May 2022
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/225193-grouping-data-for-a-3d-scatter-plot#comment_2147165
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));
?
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationColormaps
Find more on Colormaps in Help Center and File Exchange
Tags
- scatterplot
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office