Search code examples
matlabmatlab-figurecolorbarscattercolormap

How to make the colormap correspond to the vertical coordinate?


Consider the following example:

x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
figure(); scatter(x,y,[],c);
colorbar

enter image description here

In this example, the color of the data points corresponds to the horizontal coordinate. Instead, I would like color to correspond to the vertical coordinate. How can I achieve this?


Solution

  • You should use y as the color index:

    scatter(x,y,[],y);
    

    Result:

    enter image description here