필터 지우기
필터 지우기

How to assign different colors for data in different rows from a dataframe?

조회 수: 8 (최근 30일)
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

채택된 답변

Image Analyst
Image Analyst 2022년 1월 9일
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
Image Analyst
Image Analyst 2022년 2월 27일
You could call scatter several times with a different color each time, or you could use gscatter().

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Purple에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by