在 Matlab 中操作 colorbar 的底层图像数据

Manipulating underlying image data of colorbar in Matlab

在 2014 年之前的 matlab 版本中,我可以通过执行以下操作来更改颜色栏中的底层图像:

cmap = ... % something which is MxNx3
colormap(reshape(cmap, [N*M,3]))
cmapIxs2D = reshape((1:(N*M))', [N, M]);
ax = colorbar('peer', gca);
set(get(ax, 'Children'), 'CData', cmapIxs2D);
ylim(ch, [0 255]), xlim(ch, [0 1])

如果您想显示自定义颜色图,例如2D (NxMx3) 而不是普通的 1D (Nx3)。在 2014 年之后的版本中如何做到这一点,根据文档,颜色条的基础图像不再可访问,它没有子项。

示例(颜色值被解释为具有例如速度(y 轴颜色)和加速度(x 轴颜色)):

根据 OP 评论中提出的想法,我想出了 一些东西

function q38871518
%% Plot something random:
hF = figure('Color',0.4*[1 1 1],'SizeChangedFcn',@recolorCB); membrane;
hTmp = gca;
% Compute the fake colorbar contents:
cm = bsxfun(@times,permute(colormap,[1,3,2]),0:0.01:1); % figure(); imagesc(cm);
% Create an axes to hold the fake colorbar
hAx = axes(hF); imagesc(hAx,cm); axis(hAx,'off'); 
function recolorCB(varargin)
  drawnow;
  if exist('cb','var')
    cb.Face.Texture.CData(:) = 0;
    % "Link" the 'Position' prop between the colorbar and the fake colorbar:
    hAx.Position = cb.Position;
  end
end
% Create the real colorbar 
cb = colorbar(hTmp,'Color',[1 1 1]);
% Synchronize positions:
hAx.Position = cb.Position;
% Make sure the fake colorbar is at the bottom, so we can see the values clearly
uistack(hAx,'bottom');
% Final touch-ups:
drawnow; cb.Face.Texture.CData(:) = 0; cb.Face.Texture.ColorType = 'truecoloralpha';
end

结果是:

随着图形大小的变化,"fake" 颜色条移动到正确的位置。保存图形时,会出现旧的颜色条,缩小后也会出现这种情况(并且可能会执行其他一些操作)。摆脱这个需要一些额外的黑客攻击......

在 MATLAB R2016a 上测试。