Search code examples
matlabvisualizationcolorbarsurfacecolormap

How to indicate a cropped colormap


I have a surface plot colored by a given function. For clarity, the colormap does not contain all of the values of the function, so the colormap is "cropped" at the ends.
I want to indicate to the viewer that the colormap is incomplete ("cropped"), both on the colorbar itself and on the plot.

For example, take this example (MATLAB):

clearvars; clc;

x = linspace(-2,2,100);
y = linspace(-2,2,100);
[X,Y] = meshgrid(x,y);
Z = exp(-X.^2 - Y.^2);
C = (X+0.5).^2 + Y.^2;

fig = figure(1);
ax = subplot(1,1,1);
s = surf(X,Y,Z,C,'EdgeAlpha',0.2);

colorbar(ax);
ax.CLim = [0, 1];

A gaussian surface plot colored according to the two dimensional function f(x,y)=(x+0.5)^2+y^2, with a colorbar limited at 1

In this case I want to indicate that the large yellow region is not constant 1, but larger than 1, without loosing the color resolution in the blue region I get from limiting the color scale.

I didn't try anything specific, since I have no idea how to approach this problem. This is not only a coding problem ("How to code that?"), but also a question in general on cropped colormaps ("What should I do at all?").

Thanks a lot!


Solution

  • The simplest information you can add in one line is on the colorbar label:

    %% Only modify last tick label
    cb = colorbar(ax);
    cb.TickLabels{end} = '>1'
    

    This indicates that at that level of color, the values are >1:

    clipped colormap 01

    If you want something more visually prominent, you can also add a "saturated" color for all the clipped data (all data >1):

    %% and/or assign a specific color to "cropped" data
    
    % Define a color for clipped data
    % clippedColor = [.2 .2 .2] ; % very dark grey
    % clippedColor = [.9 .9 .9] ; % very light grey
    clippedColor = [1 0 0] ;    % red
    
    % Retrieve the current colormap
    cmap = colormap(fig) ;
    % Manually assign the last color to the chosen color for clipped data
    cmap(end,:) = clippedColor ; 
    % on a 256 colors colormap, we need to change the last 2 colors for matlab
    % to draw this last color in the colorbar. On a smaller colormap it
    % wouldn't be necessary
    cmap(end-1,:) = clippedColor ;
    % Assign the modified colormap to the figure
    colormap(fig,cmap)
    

    Here are 3 examples of saturated color. I tend to prefer the light versions (light grey for example) because they distract my eyes less, however they are more difficult to make out in the colorbar itself so choose what's more important for you:

    clipped colormap, saturated

    Last thing, if the jagged delineation at the saturation interface bothers you, you can apply some shading to smooth that out. Running:

    shading interp
    

    will give you a smooth figure:

    clipped colormap final