3D scatter plot with different color and legend explaining each color.

6 次查看(过去 30 天)
Hi,I want to draw a 3D scatter plot with different colors and want to put a legend that explains what each color means.
My example is something like this.
b's are the data points that I want to put in the 3D scatter. b1=[1,2,3,4,5] b2=[2,2,3,3,1] b3=[1,5,4,4,3]
s is the size of the points s=[1,1,1,1,1]
r is the color of the points (three colors) r=[0,0,2,1,2]
Finally the scatter plot is this = scatter3(b1,b2,b3,s,r,'filled')
My questions are. 1)Can I set a designate color to each numbers in r? for example, red for 0, blue for 1, green for 2.
2) how can I set a legend that explains what the color means? for example. for color 0 - 'efficient', for color 1 - 'inefficient', for color 2- 'not an eq'
  2 个评论
Steve Carr
Steve Carr 2021-7-14
Thanks for asking this Jinsoo.
My question is similar, and I will try Image Analyst's code below.
CHALLENGE: I have three column vectors of scalars, to plot the results of a Principal Components Analysis on multiple (41) individuals. These I send to a stem3D plot, with size and uniform color. Simple. I want to code individual plot elements in (6) different colors, corresponding to intervals of an index [which corresponds very nicely to one of the PC axes, was I hoped.]
I observe that Image Analyst's code will color points as successive blocks of defined size, which I could do for my data if I sorted my data matrix by index, identify the number of points in each block, write the customColorMap as below, and bobs your uncle. HOWEVER, this would be data matrix specific.
What I would like (and have tried to do) is to supply an additional column vector, where each element is the appropriate colorcode for the individual. [Essentially a discrete heat map]. Adding additional individuals (or removing outliers experimentally) is then simple.
MatLab doesn't like any of my vector formats of the form [0 0 1] etc or otherwise to try this. My two reference books don't address the problem.
Advice appreciated.
Image Analyst
Image Analyst 2021-7-14
@Steve Carr, you can make up a list of colors to apply to each marker, however it's not a column vector. It's an N-by-3 matrix of colors in the 0-1 range. If you have a column vector of something, like counts or frequency, you can turn that scalar column vector into a list of colors easily by just using the value as the row index into your colormap.
numColors = 256;
colorIndex = round(rescale(yourColumnVector, 1, numColors));
cmap = jet(numColors); % Whatever one you want.
markerColors = cmap(colorIndex, :);
markerSize = 100;
% Create the scatter plot.
scatter3(x, y, z, markerSize, markerColors, 'filled')
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);
If you still have trouble, attach your data in a new question.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-10-26
Try this:
fontSize = 20;
numPoints = 30;
b1 = randi(9, 1, numPoints);
b2 = randi(9, 1, numPoints);
b3 = randi(9, 1, numPoints);
% markerSize is the size of the markers.
markerSize = 100;
% customColorMap is the color of the points (three colors)
% Option 1: Use the built-in "jet" colormap.
% customColorMap = jet(numPoints);
% Option 2: Make up a custom one.
customColorMap = zeros(numPoints, 3); % Initialize.
% Make the first 10 points red
customColorMap(1:10, :) = repmat([1, 0, 0], 10, 1);
% Make the next 10 points dark green
customColorMap(11:20, :) = repmat([0, 0.6, 0], 10, 1);
% Make the remaining points blue
customColorMap(21:end, :) = repmat([0, 0, 1], 10, 1);
% Create the scatter plot.
scatter3(b1, b2, b3, markerSize, customColorMap,'filled')
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);
  7 个评论
Caroline
Caroline 2020-2-4
Can I ask a new question in the forum regarding this question, will it be considered a thread duplication and closed? I can't solve this problem myself ...

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2016-10-20
编辑:Image Analyst 2016-10-20
Yes, you can specify each color for every row of r. Just go ahead and make up your N by 3 r color array. For example
r = jet(length(b1));
or whatever....
By the way, you might also like to check out the function called gscatter in the stats toolbox.
  1 个评论
Jinsoo Bae
Jinsoo Bae 2016-10-20
Hi, Thank you for your reply. However, I don't understand your answer since I am a beginner in Matlab. could you give me more explanation for my question? I also need to put a legend to explain what the color means.

请先登录,再进行评论。


Jinsoo Bae
Jinsoo Bae 2016-10-26
Since no one else answers except the one I can't understand, I will just close this question by myself

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by