Scatter with different colors based on id column

조회 수: 16 (최근 30일)
Rasmus Hogslätt
Rasmus Hogslätt 2020년 12월 21일
댓글: Rasmus Hogslätt 2020년 12월 21일
I am trying to scatter a bunch of data stored in a matrix that I want to color code based on an ID. The matrix rows are of the following form:
[positionX, positionY, ID].
The matrix's rows can vary in number and there can be different amount of unique IDs. IDs are always integers in increasing order. Therefore I prefer not to split the matrix into smaller matrices based on ID.
Are there any good solutions to how this can be done? Can a condition be given to the scatter function? (I can't find this in any documentation).

채택된 답변

Ive J
Ive J 2020년 12월 21일
편집: Ive J 2020년 12월 21일
What abou this?
scatter(positionX, positionY, sz, ID, 'filled')
% example
scatter(rand(10, 1), rand(10, 1), 200, randi(5, 10, 1),...
'filled',...
'MarkerFaceAlpha', 0.7,...
'MarkerEdgeColor', 'k')
grid on
  댓글 수: 3
Ive J
Ive J 2020년 12월 21일
If you'd tried the snippet I just wrote above, you would realize each value in ID corresponds to a distinct color code on the scatter plot. For more info see 4th argument of scatter.
Below (also above example) is just an example showing the structure of your data (ID is integer).
% sample data
positionX = rand(10, 1);
positionY = rand(10, 1);
ID = randi(5, 10, 1);
sz = 200; % marker size
scatter(positionX, positionY, sz, ID,...
'filled',...
'MarkerFaceAlpha', 0.7,...
'MarkerEdgeColor', 'k')
grid on
text(positionX, positionY, string(ID), 'HorizontalAlignment', 'center') % show IDs on each marker
Rasmus Hogslätt
Rasmus Hogslätt 2020년 12월 21일
Thank you! This made me understand.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by