set colormap pivot/center

Directly manipulate an existing colormap by shifting its pivot/center to a specified value.

You are now following this Submission

Move pivot for small colormap
imagesc(peaks(1000) + 1)
CM = [.18 .07 .21; .28 .08 .39; .36 .20 .60; .37 .38 .70; .41 .53 .74;
.53 .67 .77; .74 .79 .82; .88 .84 .88; .83 .75 .71; .79 .59 .48;
.73 .41 .34; .64 .25 .31; .49 .13 .31; .31 .07 .26; .18 .07 .21];
colormap(CM); colorbar
% Move the pivot(center of colormap) to 0.
setColormapPivot(0)
% Reset colormap, then move pivot to 6.
colormap(CM);
setColormapPivot(6)
% Reset colormap, then move pivot to 10.
colormap(CM);
setColormapPivot(10)
Move pivot for large colormap
imagesc(peaks(1000) + 1)
colormap([pink;flipud(bone)]); colorbar
% Move the pivot(center of colormap) to 6.
setColormapPivot(6)
Basic usage
% Basic usage : supporting 4 calling patterns:
% 1. setColormapPivot() -> use current axes(gca) , pivot move to 0
% 2. setColormapPivot(ax) -> use specified axes , pivot move to 0
% 3. setColormapPivot(value) -> use current axes(gca) , pivot move specified value
% 4. setColormapPivot(ax, value) -> use specified axes , pivot move specified value
Other demo
% Some sample data with noise:
x = 10*rand(300, 1) - 5;
noise = 2*randn(size(x));
y = x.^2 + noise;
% A theoretical perfect x^2 line:
x_theoretical = linspace(min(x), max(x), 50);
y_theoretical = x_theoretical.^2;
% Plot the data:
figure; hold on
plot(x_theoretical, y_theoretical, 'k-')
scatter(x,y, 25, noise, 'filled')
CM = [.09 .10 .26; .04 .37 .74; .45 .66 .74; .94 .92 .92;
.81 .54 .45; .65 .13 .14; .23 .03 .07];
CX1 = linspace(0, 1, size(CM,1));
CX2 = linspace(0, 1, 256)';
CM = interp1(CX1, CM, CX2, 'pchip');
colormap(CM); cbHdl = colorbar;
ylabel(cbHdl, 'error relative to theory')
box off; axis tight; grid on
% Move the pivot(center of colormap) to 0.
setColormapPivot(0)

Cite As

Zhaoxu Liu / slandarer (2026). set colormap pivot/center (https://uk.mathworks.com/matlabcentral/fileexchange/183698-set-colormap-pivot-center), MATLAB Central File Exchange. Retrieved .

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.1.1

Updated the demo.

1.1.0

Debug: Error while executing frame: "WebGL error: 1281"

1.0.0