Search code examples
matlabcolorsmatlab-figurescatter

Scatter plot with colors from dark to bright


I want to scatter plot some data with different colors. The first row should be dark blue and for every row it should get a little bit brighter. Currently I only accomplished to make it from dark blue through some other colors to yellow.

Here is my code:

c = linspace(1,10,length(x));
sz = 25;
scatter(x,y, sz,c,'filled');
colorbar

With the resulting plot.

enter image description here

How can I make a gradual color scale from dark blue to light blue?


Solution

  • The reason your points go from blue to yellow is because they use the default colour map: parula. There are various colour maps available, but there isn't a built-in colour map for blues. However, you can easily define it yourself using an RGB-triplet:

    n = 30;  % The higher the number, the more points and the more gradual the scale
    x = rand(n,1);
    y = rand(n,1);
    c = linspace(0,1,length(x)).';  % Range from 0 to 1
    RGB = zeros(length(x),3);  % Red is zero, green is zero, blue builds up
    RGB(:,3) = c;
    sz = 25;
    
    scatter(x,y, sz,RGB,'filled');
    colormap(RGB) % Sets the correct colours for the colour bar
    colorbar
    

    black-blue

    An RGB triplet is a row-vector of three elements: [red green blue], where [0 0 0] is black and [1 1 1] is white. Leaving the first two elements at zero, and letting the third run from 0 to 1 will result in a colour scale from black to pure blue.

    Alternatively, if you want to go from black to pure blue to pure white, you can first saturate the blue as before, then leave that at 1 and increase the red and green to 1 gradually and simultaneously in the second half:

    n = 30;
    x = rand(n,1);
    y = rand(n,1);
    c = linspace(0,1,floor(length(x))./2).';  % Go to 1 in half the length
    RGB = zeros(length(x),3);
    RGB(1:floor(length(x)/2),3) = c; % Sets the blue
    RGB(floor(length(x)/2)+1:end,1) = c; % Sets the red
    RGB(floor(length(x)/2)+1:end,2) = c; % Sets the green
    RGB(floor(length(x)/2)+1:end,3) = 1; % Leaves blue at 1
    sz = 25;
    
    h1 = scatter(x,y, sz,RGB,'filled');
    colormap(RGB);
    colorbar
    

    black-blue-white