Conditional scatter in matlab

Is there a spread in the matrix (2D), where the color of the marker is determined by the third column. I can use loops and hold on, but maybe there is an easier way.

Christian

+1
source share
2 answers

The fourth scatter argument allows you to specify the color. From the documentation :

scatter (X, Y, S, C)

...

C defines the color of each marker. When C is a vector of the same length as X and Y, values ​​in C are linearly mapped to colors in the current color palette. When C is a 1 by 3 matrix, it defines marker colors as RGB values. If you have 3 points on the scatter plot and want the colors to be indices in the color palette, C should be a 3 by 1 matrix. C can also be a color string (see ColorSpec for a list of color string specifiers).

Try something like:

X = rand(1, 10); Y = rand(1, 10); colour = randi(3, 1, 10) colour = 2 1 3 1 3 1 2 2 3 1 scatter(X, Y, [], colour, 'filled'); 

enter image description here

If your datasets are large and there are several different color categories, I usually find that using plot with hold is a faster way to build.

+5
source

@ Choose the answer in order, but if you have access to the Statistics Toolbox, you can also try gscatter .

+2
source

Source: https://habr.com/ru/post/904295/


All Articles