How to assign different colors for data in different rows from a dataframe?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zhe Dong
am 9 Jan. 2022
Kommentiert: Zhe Dong
am 3 Mär. 2022
For instance, here's a dataframe
T=[label X Y
A 1 2
A 4 5
B 3 5
B 9 10]
I want to make a scatter plot with Y against X, but assign different colors and shapes based on their labels (i.e, A and B). How can I accomplish that? Many thanks!
I can think of using T{1:2,2} to extract different rows, but the real data contain many rows and more than two labels, so it's difficult to count row by row
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 9 Jan. 2022
Maybe something like untested
% letters = T{:, 1};
% x = T{:, 2};
% y = T{:, 3};
letters = {'A'; 'A'; 'B'; 'B'}
numUniqueLetters = length(unique(letters))
x = [1;4;3;9]
y = [2; 5; 5; 10];
colors = jet(numUniqueLetters)
g = findgroups(letters)
markerColors = colors(g, :)
scatter(x, y, 300, markerColors, 'filled')
grid on;
8 Kommentare
Image Analyst
am 27 Feb. 2022
You could call scatter several times with a different color each time, or you could use gscatter().
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Scatter Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!