MATLAB Colorbar-相同的颜色,比例值 [英] MATLAB Colorbar - Same colors, scaled values

查看:688
本文介绍了MATLAB Colorbar-相同的颜色,比例值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下示例:

[ 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):

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

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

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

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
. | ...

据我所知,函数 caxis ,不适合在这里使用,因为它只会显示z轴的一部分而不是整个z轴的颜色.

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.

奖金问题:如何根据X,Y和/或Z缩放颜色映射和颜色条?

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

推荐答案

[ 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值和颜色栏之间的任意映射,可以组合 contourf contour 如下(受两个很好的答案):

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 Colorbar-相同的颜色,比例值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆