Scatter with different colors based on id column

21 次查看(过去 30 天)
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by