Scatter with different colors based on id column

21 vues (au cours des 30 derniers jours)
Rasmus Hogslätt
Rasmus Hogslätt le 21 Déc 2020
Commenté : Rasmus Hogslätt le 21 Déc 2020
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).

Réponse acceptée

Ive J
Ive J le 21 Déc 2020
Modifié(e) : Ive J le 21 Déc 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 commentaires
Ive J
Ive J le 21 Déc 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 le 21 Déc 2020
Thank you! This made me understand.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by