MATLAB Colorbar - Same colors, scaled values

前端 未结 2 1262
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 06:12

Consider the following example:

[ X, Y, Z ] = peaks( 30 );
figure( 100 );
surfc( X, Y, Z );
zlabel( \'Absolute Values\' );
colormap jet;
c = colorbar( \'Loca         


        
相关标签:
2条回答
  • 2020-12-21 06:59
    [ 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' );
    

    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
    

    0 讨论(0)
  • 2020-12-21 07:00

    As I wrote in your answer, I think a better choice for showing two related values is not to create a new axis for that, but to show them one near the other. Here is 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]))
    

    0 讨论(0)
提交回复
热议问题