How to assign different colors for data in different rows from a dataframe?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Accepted Answer
Image Analyst
on 9 Jan 2022
Maybe something like untested
% letters = T{:, 1};
% x = T{:, 2};
% y = T{:, 3};
letters = {'A'; 'A'; 'B'; 'B'}
letters = 4×1 cell array
{'A'}
{'A'}
{'B'}
{'B'}
numUniqueLetters = length(unique(letters))
numUniqueLetters = 2
x = [1;4;3;9]
x = 4×1
1
4
3
9
y = [2; 5; 5; 10];
colors = jet(numUniqueLetters)
colors = 2×3
0 0 1
0 1 1
g = findgroups(letters)
g = 4×1
1
1
2
2
markerColors = colors(g, :)
markerColors = 4×3
0 0 1
0 0 1
0 1 1
0 1 1
scatter(x, y, 300, markerColors, 'filled')
grid on;

8 Comments
Zhe Dong
on 10 Jan 2022
Thanks very much for your answer! it works good, is there a way that I can specify the colors instead of using the defult ones?
Zhe Dong
on 10 Jan 2022
And seems in this way I can't assign different shapes to different labels?
The colors are what you tell it to use. Like I did:
colors = jet(numUniqueLetters)
jet is one of many colormaps you can use. See the colormap and colorbar documentation for other built-in colormaps. Or you can make up your own N-by-3 matrix of custom colors.
To have different shapes, you'd need to call scatter() once per shape. So get a subset of x and y then call scatter for that subset. To get x and y for set #1:
x1 = x(g == 1);
y1 = y(g == 1);
scatter(x1, x2, 300, markerColors, 'filled', 'Marker', 'd') % Use diamond marker.
If that works, are you ready to "Accept this answer"?
Zhe Dong
on 11 Jan 2022
In subset of x and y like your example, I can no longer use the 'markerColor' because of the lengh inconsistency, but I figured it out. Anyway, thanks!
Well you'd do this
markerColors = colors(g == 1, :)
Zhe Dong
on 27 Feb 2022
Hi sorry to bother you again, in the example you presented above, how can I add legends to different groups (i.e., different colored dots)? coz it seems that matlab sees these dots as the same group when I try to add legends, thanks in advance!
Image Analyst
on 27 Feb 2022
You could call scatter several times with a different color each time, or you could use gscatter().
Zhe Dong
on 3 Mar 2022
Thanks a lot, that helps!
More Answers (0)
Categories
Find more on Scatter Plots in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!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
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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
- United Kingdom (English)