How to change the median value on colorbar

Hi All,
I am looking to adjust the linearity of my colorbar by changing the value of the median value For example, if colorbar ranges from [-6 6], the median is 0. I want to shift it to +1 or -1).
Is there a one-line solution to this or would I need to create a customized colorbar? If the latter, how would I go about doing that?
Thanks.

 Accepted Answer

Hi Vince. The answer is you can do this, but you probably don't want to. I think it would mean developing a mathematical transfer function to scale your C data, plot the scaled data, then label the yticks on the colorbar, which will be scaled, but use labels that indicate the unscaled values.
For a simple example, here's what I mean, but simply scaling by a factor of two:
Z = peaks;
figure
subplot(1,2,1)
imagesc(Z)
caxis([-6 6])
colorbar
Z2 = 2*Z;
subplot(1,2,2)
imagesc(Z2)
caxis([-6 6]*2)
cb = colorbar;
set(cb,'ytick',[-12 0 12]','yticklabel',[-6 0 6]')
That works, but it's overly complicated even for a simple scaling without moving the central value around. What you are proposing seems more complicated, more prone to error, and less intuitive for the viewer to understand. In general, if you ever find yourself wanting to do such funny things with the colorbar, or if you find yourself inventing complicated colormaps to show what you want to show, it's probably a good time to step back and rethink what you're doing.

7 Comments

Hi Chad,
Thanks for the response. If you are familiar with Ocean Data View, I am basically trying to replicate the nonlinearity scaling tool here. Everyone I've spoken too said it is pretty difficult and maybe not worth the effort. I tried using a monochrome colorbar from cmocean, but it doesn't depict the data as well as a contrasting colorbar (balance). If I could just shift the median value in balance, it'd be perfect.
I'll give this code you provided a go, but if I hit a roadblock I'll have to take a step back like you suggested.
Cheers.
If you're using cmocean('balance') you can change the divergence point of the colormap with the 'pivot' option. So for example if you want white to appear at some arbitrary value, let's say 2.3, do this:
imagesc(peaks)
colorbar
cmocean('balance','pivot',2.3)
Perfect. This is exactly what I was trying to do--thanks! Will this pivot point also work on monochromatic colormaps too?
I tried using the pivot tool, however it is flushing everything out to red or blue. My data are all either positive or negative, but the diverging colormap shows a great contrast (changes in water structure, ventilation, etc.). It doesn't look like cmocean defaulted to zero here, but rather the mean of the data limits. Will pivot not work here?
For example, the first image is the default setting. Notice how the divergent point is centered at the mean of the data range. The second image is after I try to pivot the diverging point to -1.1 and 1.1 in the top and bottom plots, respectively. Any thoughts?
It would be a easier to diagnose if you could provide a minimal working example. I wonder if you're getting the +/- sign of the pivot value mixed up? For example, here's something similar to your top plot using the default settings (pivot value is the center):
imagesc(peaks(500)/12-1.2)
caxis([-1.8 -0.4])
colorbar
cmocean('bal')
Now try this, which is what I think you want:
cmocean('bal','pivot',-1.1)
But I wonder if this is what you did instead:
cmocean('bal','pivot',1.1)
I tried switching the +/- on the pivot mark, but now I'm thinking it's because I haven't set colorbar limits--I let it set itself based on the data. Here's an example of the code (without changing the pivot point):
cmocean('-balance',75);
pcolor(T,Z,dd18o);
shading interp;
set(gca,'YDir','reverse','XMinorTick','on');
colorbar;
c = colorbar;
title(c,['(' char(8240) ')'],'FontSize',12);

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!