首页 文章

MATLAB Colorbar - 相同颜色,缩放值

提问于
浏览
2

请考虑以下示例:

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

输出如下:
MATLAB Colorbar - Absolute Scaling

我怎么能 scale the ticks on the colorbar, i.e. scale the c-axis (例如将值除以100):

  • 而不更改绘图上的z值和颜色

  • 而不更改颜色条上的颜色

  • 不改变绘图上颜色之间的关系,颜色条上的颜色和绘图的z值

  • ,同时仍然使用全部颜色条

在上图中,我想缩放c轴,使其显示相关z的这个值:

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

根据我的理解,函数caxis在这里不适合,因为它只显示z轴子部分的颜色而不是整个z轴的颜色 .

奖金问题:如何将颜色映射和颜色条缩放为X,Y和/或Z的函数?

2 回答

  • 1
    [ 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

    对于z值和颜色条之间的任意映射,可以将surfcontourfcontour组合如下(灵感来自these two很棒的答案):

    [ 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

  • 2

    正如我写的in your answer,我认为显示两个相关值的更好选择不是为此创建一个新轴,而是将它们显示在另一个附近 . 这是一个建议:

    [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

相关问题