2
votes

I'm trying to generate a scatter plot in matlab with a colorbar with minimum values colored in blue and the maximum in red. I can do this with the redblue colomap from the file exchange (e.g. http://www.mathworks.com/matlabcentral/fileexchange/25536-red-blue-colormap)

Here's an example:

dd = [70.7  140.0   902.0
70.2    138.6   765.0
68.4    141.2   645.0
67.8    133.2   735.0
64.9    137.7   398.0
63.8    145.1   583.0
74.6    126.6   1880.0
82.8    124.3   -71.0
91.8    124.7   1315.0
93.1    119.6   400.0
95.7    117.0   326.0
104.3   121.3   179.0
104.4   122.0   205.0
104.4   122.3   181.0
104.6   113.0   265.0
109.8   118.2   170.0
55.0    149.6   913.0];

scatter(dd(:,1),dd(:,2),80,dd(:,3),'filled');
box on;
colormap(redblue);
c = colorbar;

I would like to alter the colorbar so that the white region refers to zero. this can be done by altering the caxis to:

caxis([-1800 1800]);

But this doesn't make sense in this example because the majority of the points are above 0. Therefore, I was hoping to find a way of having the colorbar from -200 to 1800 but with the white in the colorbar stillbeing as zero. Is this possible?

1

1 Answers

2
votes

Here is the updated code

dd = [70.7  140.0   902.0
70.2    138.6   765.0
68.4    141.2   645.0
67.8    133.2   735.0
64.9    137.7   398.0
63.8    145.1   583.0
74.6    126.6   1880.0
82.8    124.3   -71.0
91.8    124.7   1315.0
93.1    119.6   400.0
95.7    117.0   326.0
104.3   121.3   179.0
104.4   122.0   205.0
104.4   122.3   181.0
104.6   113.0   265.0
109.8   118.2   170.0
55.0    149.6   913.0];

scatter(dd(:,1),dd(:,2),80,dd(:,3),'filled');
box on;

%% color map begin
m = size(get(gcf,'colormap'),1);

    m2 = floor(m*0.9);
    r2 = ones(m2,1);
    b2 = flipud((0:m2-1)'/max(m2-1,1));
    g2 = b2;

    m1 = m-m2;
    r1 = (0:m2-1)'/max(m2-1,1);
    r1(1:(m2-m1))=[];
    g1 = r1;
    b1 = ones(m1,1);

    r = [r1; r2];
    g = [g1; g2];
    b = [b1; b2];
    cm = [r g b]; 

%% color map end

colormap(cm);
c = colorbar;
caxis([-200 1800]);

If you don't want your colormap to be symmetric. Then use this

dd = [70.7  140.0   902.0
70.2    138.6   765.0
68.4    141.2   645.0
67.8    133.2   735.0
64.9    137.7   398.0
63.8    145.1   583.0
74.6    126.6   1880.0
82.8    124.3   -71.0
91.8    124.7   1315.0
93.1    119.6   400.0
95.7    117.0   326.0
104.3   121.3   179.0
104.4   122.0   205.0
104.4   122.3   181.0
104.6   113.0   265.0
109.8   118.2   170.0
55.0    149.6   913.0];

scatter(dd(:,1),dd(:,2),80,dd(:,3),'filled');
box on;

%% color map begin
m = size(get(gcf,'colormap'),1);

    m2 = floor(m*0.9);
    r2 = ones(m2,1);
    b2 = flipud((0:m2-1)'/max(m2-1,1));
    g2 = b2;

    m1 = m-m2;
    r1 = (0:m1-1)'/max(m1-1,1);
    g1 = r1;
    b1 = ones(m1,1);

    r = [r1; r2];
    g = [g1; g2];
    b = [b1; b2];
    cm = [r g b]; 

%% color map end

colormap(cm);
c = colorbar;
caxis([-200 1800]);