【问题标题】:Matlab colormap & caxis matching nonlinear plotsMatlab 颜色图和 caxis 匹配非线性图
【发布时间】:2019-08-15 21:41:02
【问题描述】:

我正在尝试调整我的数据以匹配下面发布的地图(左图),但无法找出颜色条的正确代码。增量是非线性的。

这是我拥有的相关代码(它是用于空间映射的非常大代码的一部分,因此很遗憾无法使其可重现):

caxes.donatt = [0 2.5];
colormaps.donat = mf_colormap_cpt('BlWhRe', 8);
colormaps.donat(1:1,:)  = [0.00 0.00 0.50];
colormaps.donat(2:2,:)  = [0.00 0.50 1.00];
colormaps.donat(3:3,:)  = [0.67 0.90 0.93];
colormaps.donat(4:4,:)  = [1.00 1.00 1.00];
colormaps.donat(5:5,:)  = [1.00 0.87 0.68];
colormaps.donat(6:6,:)  = [0.98 0.67 0.38];
colormaps.donat(7:7,:)  = [1.00 0.40 0.10];
colormaps.donat(8:8,:)  = [1.00 0.00 0.00];

这是颜色条预期结果的图片: expected result

这是我使用上面显示的代码的当前结果: current colorbar result

【问题讨论】:

  • 可能相关:@gnovice 提供了一个非常有用的answer 关于颜色条

标签: matlab colormap color-mapping


【解决方案1】:

这里的问题是您尝试模仿的颜色图是非线性的,但 MATLAB 以线性方式将数据映射到颜色图。您可以通过首先使用histcounts 对数据进行分箱以创建线性映射来解决此问题,然后调整color bar ticks labels 以使您的线性颜色映射显示为非线性。这是一个例子:

data = 2.5.*rand(200);         % Sample random data in the range [0 2.5]
edges = [0 0.5 0.75 0.9 1.1 1.25 1.5 2 2.5];  % Define edges of nonlinear map
imgMap = [0.00 0.00 0.50; ...  % Colormap colors
          0.00 0.50 1.00; ...
          0.67 0.90 0.93; ...
          1.00 1.00 1.00; ...
          1.00 0.87 0.68; ...
          0.98 0.67 0.38; ...
          1.00 0.40 0.10; ...
          1.00 0.00 0.00];

[~, ~, bin] = histcounts(data, edges);  % Bin the data according to the edges

image(bin);                  % Plot bin index, not the original data
colormap(imgMap);            % Add new colormap
hBar = colorbar;             % Add color bar
set(hBar, 'TickLabels', ...  % Modify color bar tick labels
    [{''}; cellstr(num2str(edges(2:(end-1)).', '%g')); {''}]);

这是示例图:

请注意,如果您的数据包含 NaN 值,您将得到一个为 0 的 bin value,在上面的示例中,它将映射到颜色映射范围内的最小值(这是 imgMap 的第一个元素)。要重构这些NaN 值,请在将数据与histcounts 合并后执行以下操作:

bin(bin == 0) = nan;

【讨论】:

  • 嗨,这很好用 - 但现在的问题是 - 所以我的数据有 NaN(没有观察到温度),正如你在我之前的“当前颜色条”中看到的那样,它应该呈灰色结果'链接。但是现在,一旦我对这些值进行分类,它们就会变成零,如果我阻止所有的零,那就太好了。我将尝试在下面正确地写这个。如果您有任何很棒的建议
  • @gall:编辑提及如何处理NaNs。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
  • 2015-08-19
  • 2018-06-22
  • 1970-01-01
相关资源
最近更新 更多