9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jinsoo Bae am 20 Okt. 2016
Kommentiert: Image Analyst am 14 Jul. 2021
Akzeptierte Antwort: Image Analyst
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 Kommentare Keine anzeigenKeine ausblenden
Keine anzeigenKeine ausblenden
Steve Carr am 14 Jul. 2021
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_1637228
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 am 14 Jul. 2021
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_1637303
In MATLAB Online öffnen
@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.
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Image Analyst am 26 Okt. 2016
In MATLAB Online öffnen
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 Kommentare 5 ältere Kommentare anzeigen5 ältere Kommentare ausblenden
5 ältere Kommentare anzeigen5 ältere Kommentare ausblenden
Jinsoo Bae am 26 Okt. 2016
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_401902
Thank you. Now I understand that Matlab specify with three numbers. I accepted your answer. Could you also tell me how to put an legend for each color? Red-inefficient, blue-efficient, Green- not an eq
Image Analyst am 26 Okt. 2016
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_401919
In MATLAB Online öffnen
I'm not sure. The legend function doesn't seem to do that. You can use text:
text(1, 1, 'Red-inefficient, Blue-efficient, Green- not an eq');
Use sprintf() to put the markers into the text string if you want them. Use the 'ForegroundColor' option if you want multiple text in different colors.
Caroline am 31 Jan. 2020
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_792268
Bearbeitet: Image Analyst am 31 Jan. 2020
In MATLAB Online öffnen
I would like to add 5 colors - I add a color every 5 rows, but I have a problem with the size of the right side
numPoints = 25;
KolorMap = zeros(numPoints, 5); % Initialize.
KolorMap(1:5, :) = repmat([0.450, 0.419, 0.980], 5, 1);
KolorMap(6:10, :) = repmat([0.305, 0.270, 0.890], 5, 1);
KolorMap(11:15, :) = repmat([0.098, 0.058, 0.760], 5, 1);
KolorMap(16:20, :) = repmat([0.207, 0.184, 0.596], 5, 1);
KolorMap(21:25, :) = repmat([0.086, 0.066, 0.435], 5, 1);
And I have error:
Unable to perform assignment because the size of the left side is 5-by-5 and the size of the right side is 1-by-3.
I read a doc for repmat but I don't understand it..
x, y and z to scatter3 I have already defined and works.
And the other questions: Is it possible to change markertype, that there were 5 types in? Because I want that x change in color, y change in markertype, z is neutral
Thank you for any answers!
Image Analyst am 31 Jan. 2020
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_792276
In MATLAB Online öffnen
You need to initialize to 3 columns, not 5. But it's better to use repelem():
numPoints = 25;
KolorMap = zeros(numPoints, 3); % Initialize with 3 columns.
KolorMap(1:5, :) = repmat([0.450, 0.419, 0.980], 5, 1); % Replicate row 5 times.
KolorMap(6:10, :) = repmat([0.305, 0.270, 0.890], 5, 1); % Replicate row 5 times.
KolorMap(11:15, :) = repmat([0.098, 0.058, 0.760], 5, 1); % Replicate row 5 times.
KolorMap(16:20, :) = repmat([0.207, 0.184, 0.596], 5, 1); % Replicate row 5 times.
KolorMap(21:25, :) = repmat([0.086, 0.066, 0.435], 5, 1) % Replicate row 5 times.
% The better way, using the new repelem() function:
colors = [0.450, 0.419, 0.980; % Initialize colors - one row per color.
0.305, 0.270, 0.890;
0.098, 0.058, 0.760;
0.207, 0.184, 0.596;
0.086, 0.066, 0.435]
% Replicate each row 5 times.
KolorMap = repelem(colors, 5, 1)
Caroline am 3 Feb. 2020
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_793009
- daneMeanAll.mat
- scatter3.jpg
Unfortunately I can't deal with this problem. When I import data without changing colors it works, but when it indicates colors - S must be a scalar or a vector the same length as X ..
But my vectors look the same as those from your example, except there are 25 :(
Caroline am 4 Feb. 2020
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_793460
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 am 4 Feb. 2020
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_793557
Sure, go ahaead.
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (2)
Image Analyst am 20 Okt. 2016
Bearbeitet: Image Analyst am 20 Okt. 2016
In MATLAB Online öffnen
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 Kommentar -1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Jinsoo Bae am 20 Okt. 2016
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/308326-3d-scatter-plot-with-different-color-and-legend-explaining-each-color#comment_400311
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.
Melden Sie sich an, um zu kommentieren.
Jinsoo Bae am 26 Okt. 2016
Since no one else answers except the one I can't understand, I will just close this question by myself
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAnnotations
Mehr zu Annotations finden Sie in Help Center und File Exchange
Tags
- 3d plots
- scatter plot
- colormap with scatterplot
Produkte
- Statistics and Machine Learning Toolbox
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung