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

6 vues (au cours des 30 derniers jours)
Jinsoo Bae
Jinsoo Bae le 20 Oct 2016
Commenté : Image Analyst le 14 Juil 2021
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 commentaires
Steve Carr
Steve Carr le 14 Juil 2021
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 le 14 Juil 2021
@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.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 26 Oct 2016
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 commentaires
Caroline
Caroline le 4 Fév 2020
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 ...
Image Analyst
Image Analyst le 4 Fév 2020
Sure, go ahaead.

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 20 Oct 2016
Modifié(e) : Image Analyst le 20 Oct 2016
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 commentaire
Jinsoo Bae
Jinsoo Bae le 20 Oct 2016
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.

Connectez-vous pour commenter.


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

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by