Search code examples
matlabmatlab-figureaxescolorbar

MATLAB Colorbar - Same colors, scaled values


Consider the following example:

[ X, Y, Z ] = peaks( 30 );
figure( 100 );
surfc( X, Y, Z );
zlabel( 'Absolute Values' );
colormap jet;
c = colorbar( 'Location', 'EastOutside' );
ylabel( c, 'Relative Values' );

The output looks as follows: MATLAB Colorbar - Absolute Scaling

How can I scale the ticks on the colorbar, i.e. scale the c-axis (e.g. divide the values by 100):

  • without changing the z values and colors on the plot
  • without changing the colors on the colorbar
  • without changing the relation between the colors on the plot, the colors on the colorbar and the z values of the plot
  • while still using the full range of the colorbar

In the picture above, I would like to scale the c-axis such that it shows this values for the related z:

z | c-axis
----------
8 | 8/100
6 | 6/100
4 | 4/100
. | ...

The function caxis, as I understand it, is not suited here, as it would just show the colors for a subsection of the z-axis and not for the whole z-axis.

Bonus question: How could one scale the color mapping and the colorbar as a function of X, Y and/or Z?


Solution

  • [ X, Y, Z ] = peaks( 30 );
    figure( 101 );
    surfc( X, Y, Z );
    zlabel( 'Absolute Values' );
    %
    Z_Scl = 0.01;
    Z_Bar = linspace( min(Z(:)), max(Z(:)), 10 );
    %
    colormap jet;
    c = colorbar( 'Location', 'EastOutside', ...
        'Ticks', Z_Bar, 'TickLabels', cellstr( num2str( Z_Bar(:)*Z_Scl, '%.3e' ) ) );
    ylabel( c, 'Relative Values' );
    

    MATLAB Colorbar - Scaled Colorbar - Version 1

    For an arbitrary mapping between the z-values and the colorbar, it is possible to combine surf, contourf and contour as follows (inspired by these two great answers):

    [ X, Y, Z ] = peaks( 30 ); % Generate data
    CB_Z = (sin( Z/max(Z(:)) ) - cos( Z/max(Z(:)) )).^2 + X/5 - Y/7; % Generate colormap
    CF_Z = min( Z(:) ); % Calculate offset for filled contour plot
    CR_Z = max( Z(:) ); % Calculate offset for contour plot
    %
    figure( 102 ); % Create figure
    clf( 102 );
    hold on; grid on; grid minor; % Retain current plot and create grid
    xlabel( 'x' ); % Create label for x-axis
    ylabel( 'y' ); % Create label for y-axis
    zlabel( 'Scaling 1' ); % Create label for z-axis
    surf( X, Y, Z, CB_Z ); % Create surface plot
    %
    CF_H = hgtransform( 'Parent', gca ); % https://stackoverflow.com/a/24624311/8288778
    contourf( X, Y, CB_Z, 20, 'Parent', CF_H ); % Create filled contour plot
    set( CF_H, 'Matrix', makehgtform( 'translate', [ 0, 0, CF_Z ] ) ); % https://stackoverflow.com/a/24624311/8288778
    %
    CR_H = hgtransform( 'Parent', gca ); % https://stackoverflow.com/a/24624311/8288778
    contour( X, Y, CB_Z, 20, 'Parent', CR_H ); % Create contour plot
    set( CR_H, 'Matrix', makehgtform( 'translate', [ 0, 0, CR_Z ] ) ); % https://stackoverflow.com/a/24624311/8288778
    %
    colormap jet; % Set current colormap
    CB_H = colorbar( 'Location', 'EastOutside' ); % Create colorbar
    caxis( [ min( CB_Z(:) ), max( CB_Z(:) ) ] ); % Set the color limits for the colorbar
    ylabel( CB_H, 'Scaling 2' ); % Create label for colorbar
    

    MATLAB Surf Contourf Contour Colorbar