【问题标题】:Trying to apply a color map to a bar graph in MATLAB尝试将颜色图应用于 MATLAB 中的条形图
【发布时间】:2017-11-07 02:44:43
【问题描述】:

我有一组数据,我试图将其绘制为直方图。此外,我想根据 x 轴位置对各个条形进行着色。 CData,这里描述的似乎做我想做的,但我不能让它工作。

这是我的代码:

h = bar(new_edge,N,'hist','FaceColor','flat');
hold on

for n = 1:length(N)
    if (x - x/1.09) - (x-1) > 0
        probability(n) = 1 - ((x-x/1.09) - (x-1))/((x - 1/1.09)+(x/0.91 - x)); 
    else
        probability(n) = 1;
    end

    color_num = 30;
    cm = jet(color_num);
    min = 0.5450;
    max = 1;

    color_map_index = floor(1 + (probability(n) - min)/(max-min)*(color_num-1));
    rbg = cm(color_map_index,:);

    h.CData(n,:) = rbg;

end

与 MATLAB 示例类似,我首先创建我的条形图。接下来,我想循环并根据计算为每个条形指定颜色。我通过创建一个带有# of bins 和一个 min/max 的颜色图,获取一个颜色索引,然后最终检索 rbg 值来做到这一点。尝试应用颜色时出现以下错误:

下标索引必须是正整数或逻辑数。

h.CData(n,:) = rbg;

如果我深入研究 h 对象,MATLAB 会告诉我 CData 的大小为 (4x65)。这里发生了什么? new_edgeN 都是 1x65 向量。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    你确定你得到的错误(“下标索引必须是真正的正整数或逻辑”)来自以下行吗?:

    h.CData(n,:) = rbg;
    

    既然我们知道n 是一个大于等于一的正整数,那么这里的索引应该没有问题。您的错误更有可能来自它上面的行(即color_map_index 的值小于 1)。我会仔细检查你是如何计算color_map_index的。

    您也可以尝试使用function notation(即getset)而不是dot notation 来更新属性:

    cData = get(h, 'CData');
    cData(n, :) = rbg;
    set(h, 'CData', cData);
    

    顺便说一句,你也不应该给你的变量赋予与现有函数相同的名称,就像你在这里所做的那样:

    ...
    min = 0.5450;
    max = 1;
    ...
    

    这会影响内置的 minmax 函数,它们是 can also lead to the same error message under other conditions。一定要重命名那些。

    如果您在尝试这些修复后仍然遇到问题,您可以尝试使用索引颜色映射来设置颜色,如one of my other answers(靠近底部)所示。作为一个简单的例子,下面绘制了 30 个不同可能值的 20 个条形图,然后根据它们的高度对它们进行着色:

    color_num = 30;
    N = randi(color_num, 1, 20);
    hBar = bar(N, 'hist');
    colormap(parula(color_num));
    set(hBar, 'CData', N, 'CDataMapping', 'direct');
    

    还有剧情:

    【讨论】:

      【解决方案2】:

      这可能是您的 Matlab 版本的问题。 当我在 2017b 上用 bar 测试 CData 时,这有效:

      openExample('graphics/ControlIndividualBarColorsExample')
      

      当我在 2017a 上尝试时,它没有运行。 该示例是否有效?

      【讨论】:

      • 我想你已经开始了。该示例也不起作用,因此这必须是版本问题。谢谢!
      【解决方案3】:

      鉴于这是一个版本控制问题,确实没有一个干净的解决方案。万一其他人提出类似的问题并使用相同的版本,这是一个在 2017a 年对我有用的解决方法。

      您可以简单地绘制矩形,而不是创建条形图。它很混乱,但它确实产生了所需的result

      [N,edges] = histcounts(AB(AB<2));
      
      probability = zeros(1,length(N));
      new_edge = zeros(1,length(N));
      
      for m = 1:length(N)
          new_edge(m) = (edges(m) + edges(m+1))/2;
      end
      
      figure
      hold on
      
      for n = 1:length(N)
          x = new_edge(n);
          if ((x - x/1.09) - (x-1)) > 0
              probability(n) = 1 - ((x-x/1.09) - (x-1))./((x - x/1.09)+(x/0.91 - x)); 
          else
              probability(n) = 1;
          end
      
          color_num = 100;
          cm = jet(color_num);
          min = 0;
          max = 1;
      
          color_map_index(n) = floor(1 + (probability(n) - min)/(max-min)*(color_num-1));
          rbg = cm(color_map_index(n),:);
      
          rectangle('Position',[edges(n),0,(edges(n+1)-edges(n)),N(n)],'Edgecolor','k','FaceColor',rbg)
      
      end
      
      set(gcf,'color','w');
      blah = colorbar;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-01
        • 2018-04-04
        • 2018-10-28
        • 2015-02-03
        相关资源
        最近更新 更多