Scatter with different colors based on id column

21 visualizzazioni (ultimi 30 giorni)
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).

Risposta accettata

Ive J
Ive J il 21 Dic 2020
Modificato: Ive J il 21 Dic 2020
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 Commenti
Ive J
Ive J il 21 Dic 2020
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 il 21 Dic 2020
Thank you! This made me understand.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by