Change colorbar scale, but keep zero value white

I am trying to plot positive data as red (darker red=higher magnitude) and negative data as blue. I want zero to be white. I am using the bluewhitered function to color the data because I could not have another colormap that did this in the matlab choices. But since my data include much higher positive magnitudes than negative values. The negatives look dark blue even at low magnitude. I think the function must make the max and min data values the darkest color end points. I tried to force the negative data to show up lighter blue by using caxis to limit the colors from -0.5 to +0.5 but then zero is not white for some reason. Do you have another idea for a similar color map or a way to fix this one?
I have attached the first plot and my attempted fix.
Here's my code:
f1=figure(1)
pcolor(MX,PK.mab,PK.fixdeltaPH);
shading('interp');
colormap(bluewhitered);
ylabel('Depth (mab)','fontsize',16);
c=colorbar();
%caxis([-0.5 0.5]); % This did not fix it because zero is not white now
ylim([0 6])
title(c,'\DeltapH','fontsize',16);
set(gca,'fontsize',16);
x = datenum('July-23-2018'):7:datenum('Oct-5-2018');
set(gca, 'XTick', x);
datetick('x','mm/dd','keepticks')

3 Comments

FYI, bluewhitered colormap is on FEX, link below
Could you create a minimal working example that recreates the problem (something we can copy-paste and run)?
Try loading this mat file and running this:
load data % includes times, dephts, and delta pH values
f1=figure(1)
%pcolor(MX,PK.mab,PK.fixdeltaPH);
pcolor(times,depths,delPH);
shading('interp');
colormap(bluewhitered);
ylabel('Depth (mab)','fontsize',16);
c=colorbar();
%caxis([-0.5 0.5]);
ylim([0 6])
title(c,'\DeltapH','fontsize',16);
set(gca,'fontsize',16);
x = datenum('July-23-2018'):7:datenum('Oct-5-2018');
set(gca, 'XTick', x);
datetick('x','mm/dd','keepticks')
I ran your test script and it looks like it worked OK for me. The zero values were white on my plot.

Sign in to comment.

 Accepted Answer

Yes it works if I don't include (currently commented out) the caxis line. The issue is the negatives look more extreme since the minimum data value is automatically the darkest blue. It is when I use the caxis([-0.5 0.5]) line (to try to better distribute shading) that I get non-white zero. White ends up being between -2.0 and -3.0 on the color bar (see 'attempted_fix.png'). Thank you!

3 Comments

Try to move the caxis command above the colormap setting.
That worked! Thank you so much!
Excellent! Glad to help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!