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 like this: MATLAB Colorbar - Absolute Scaling

How can I scale ticks on a color bar i.e. scale the c axis (for example, divide values ​​by 100):

  • without changing z values ​​and colors on the graph
  • without changing colors on the color bar
  • without changing the relationship between the colors in the plot, the colors in the color bar, and the z-values ​​of the plot
  • when using the full range of the color scale

In the picture above, I would like to scale the c-axis so that it shows these values ​​for the corresponding z:

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

      

The function caxis

, as I understand it, is not suitable here, since it will simply show the colors for the subsection of the z-axis, and not for the entire z-axis.

Bonus question: how can the color display and color bar be scaled as a function of X, Y and / or Z?

+2


source to share


2 answers


[ 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 Color Bar - Version 1

For arbitrary mapping between z values ​​and a color bar can be combined surf

, contourf

and contour

as follows (inspired by these two excellent 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 ); % /questions/901054/plot-multiple-2d-contour-plots-in-one-3d-figure-matlab/3212833#3212833
contourf( X, Y, CB_Z, 20, 'Parent', CF_H ); % Create filled contour plot
set( CF_H, 'Matrix', makehgtform( 'translate', [ 0, 0, CF_Z ] ) ); % /questions/901054/plot-multiple-2d-contour-plots-in-one-3d-figure-matlab/3212833#3212833
%
CR_H = hgtransform( 'Parent', gca ); % /questions/901054/plot-multiple-2d-contour-plots-in-one-3d-figure-matlab/3212833#3212833
contour( X, Y, CB_Z, 20, 'Parent', CR_H ); % Create contour plot
set( CR_H, 'Matrix', makehgtform( 'translate', [ 0, 0, CR_Z ] ) ); % /questions/901054/plot-multiple-2d-contour-plots-in-one-3d-figure-matlab/3212833#3212833
%
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

+2


source


As I wrote in your answer , I believe that the best choice for displaying two related values ​​is not to create a new axis for that, but to show them side by side with the other. Here's a suggestion:

[X,Y,Z] = peaks(30);
surfc(X,Y,Z);
zlabel('Absolute (Relative) Values');
colormap jet
Z_Scl = 0.01;
zticks = get(gca,'ZTick');
set(gca,'ZTickLabel',sprintf('%g (%g)\n',[zticks;zticks.*Z_Scl]))

      



enter image description here

+1


source







All Articles