【问题标题】:MATLAB Colorbar - Same colors, scaled valuesMATLAB Colorbar - 相同的颜色,缩放值
【发布时间】:2017-07-24 15:58:13
【问题描述】:

考虑以下示例:

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

输出如下所示:

我如何缩放颜色条上的刻度,即缩放 c 轴(例如,将值除以 100):

  • 不改变绘图上的 z 值和颜色
  • 不改变颜色条上的颜色
  • 不改变绘图上的颜色、颜色条上的颜色和绘图的 z 值之间的关系
  • 同时仍使用颜色栏的全部范围

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

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

据我了解,caxis 函数不适合这里,因为它只会显示 z 轴的一部分而不是整个 z 轴的颜色。

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

【问题讨论】:

    标签: matlab matlab-figure axes colorbar


    【解决方案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' );
    

    对于 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
    

    【讨论】:

    • 您希望 z 轴中的值与 c 轴中的值不同吗?这有点误导......
    • @EBH 问题更新了,现在清楚还是误导?
    • 您的意图甚至在以前就很清楚,在我看来,将 2 个轴显示为相同的值似乎是多余的或误导性的。但我想它可能对某些可视化有用(我认为宁愿将其中一个值放在 z 轴内的括号中,例如 5 (0.05))。
    • @EBH 谢谢。是的,你是对的,括号是个好主意。一种可能性是在 z 轴上使用 SI 单位,在颜色条上使用美制或英制单位,只要确保线性缩放。现在我考虑了一下,将第四个参数 (C) 与 surfc 结合使用 caxis 命令可能是更好的方法,请参阅更新后的答案。
    【解决方案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]))
    

    【讨论】:

    • 这里不鼓励 MWE 吗?我仍然相信添加行'ax = gca;'可能会帮助没有经验的用户,因为它不会按原样运行。
    • @user5564832 你说得对!抱歉,我混合了我的代码的 2 个版本而忘记修复它。现已修复。
    • 完美,无论如何你的版本看起来更干净,感谢您的回答,好主意。
    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2013-02-05
    • 2016-10-19
    • 2019-03-14
    • 2018-11-17
    相关资源
    最近更新 更多