【问题标题】:Matlab: 1D array to RGB triplets with colormapMatlab:带有颜色图的一维数组到 RGB 三元组
【发布时间】:2017-02-24 17:43:44
【问题描述】:

我正在尝试绘制一组矩形,每个矩形都有一个填充颜色,表示介于 0 和 1 之间的某个值。理想情况下,我想使用任何标准颜色图。

请注意,矩形没有放置在漂亮的网格中,因此使用imagescsurf 或类似的方法似乎不切实际。此外,scatter 函数似乎不允许我分配自定义标记形状。因此,我坚持在for循环中绘制一堆矩形并手动分配FillColor

从标量值计算 RGB 三元组最有效的方法是什么?我一直无法找到符合[r,g,b] = val2rgb(value,colormap). 的函数现在,在检查rgbplot(jet) 之后,我已经构建了一个计算“jet”值的函数。这似乎有点傻。当然,我可以通过插值从任意颜色图中获取值,但这对于大型数据集来说会很慢。

那么,高效的[r,g,b] = val2rgb(value,colormap) 会是什么样子?

【问题讨论】:

    标签: matlab colormap


    【解决方案1】:

    您还有另一种处理方法:使用patchfill 绘制矩形,指定色阶值C 作为第三个参数。然后就可以添加和调整颜色条了:

    x = [1,3,3,1,1];
    y = [1,1,2,2,1];
    figure
    for ii = 1:10
        patch(x + 4 * rand(1), y + 2 * rand(1), rand(1), 'EdgeColor', 'none')
    end
    colorbar
    

    有了这个输出:

    【讨论】:

      【解决方案2】:

      我认为 erfan 的补丁解决方案比我的矩形方法更优雅和灵活。

      无论如何,对于那些寻求将标量转换为 RGB 三元组的人,我将在此问题上添加我的最终想法。我解决问题的方法是错误的:颜色应该从颜色图中最接近的匹配中绘制,而不需要插值。解决方案变得微不足道;我为那些将来偶然发现这个问题的人添加了一些代码。

      % generate some data
      x = randn(1,1000);
      
      % pick a range of values that should map to full color scale
      c_range = [-1 1];
      
      % pick a colormap
      colormap('jet');
      
      % get colormap data
      cmap = colormap;
      
      % get the number of rows in the colormap
      cmap_size = size(cmap,1);
      
      % translate x values to colormap indices
      x_index = ceil( (x - c_range(1)) .* cmap_size ./ (c_range(2) - c_range(1)) );
      
      % limit indices to array bounds
      x_index = max(x_index,1);
      x_index = min(x_index,cmap_size);
      
      % read rgb values from colormap
      x_rgb = cmap(x_index,:);
      
      % plot rgb breakdown of x values; this should fall onto rgbplot(colormap)
      hold on;
      plot(x,x_rgb(:,1),'ro');
      plot(x,x_rgb(:,2),'go');
      plot(x,x_rgb(:,3),'bo');
      axis([c_range 0 1]);
      xlabel('Value');
      ylabel('RGB component');
      

      结果如下:

      【讨论】:

        猜你喜欢
        • 2016-07-29
        • 1970-01-01
        • 2020-05-15
        • 2020-05-24
        • 2014-05-26
        • 2017-11-19
        • 1970-01-01
        • 1970-01-01
        • 2012-04-03
        相关资源
        最近更新 更多