Scatter with different colors based on id column
16 次查看(过去 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).
0 个评论
采纳的回答
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
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!