Search code examples
matlabloopsif-statementscatter-plot

Loop with "for" and "if" and then a scatter-plot with different colors


I would like a scatter plot with two different colors for different fields.

I have one table with x1, x2, y3. I make one scatter with scatter(x1,x2,"o","filled"), but I would like that colors were on the base of y3.

y3 has values from 0 to 81. I would like that fields with values<10 were green, vice versa values>10 were red.


Solution

  • You can use the optional fourth input of scatter to specify color. That means you also need to define the third input, which specifies marker size:

    x1 = randn(1,300);
    x2 = randn(1,300);
    y3 = rand(1,300)*81; % example data
    markersize = 20; % or specify [], which causes default marker size to be used
    scatter(x1, x2, markersize, y3<10, 'o', 'filled')
    colormap([.8 0 0; 0 .8 0]) % red and green
    

    enter image description here