【问题标题】:How to change individual bar colours of subplots in Matlab如何在 Matlab 中更改子图的各个条形颜色
【发布时间】:2014-10-09 01:51:56
【问题描述】:

我有以下代码:

%initialize variables
titles = {'1a', '1b', '2a', '2b', '3a', '3b', '4a', '4b', '5a', '5b', '6a', '6b', '7a', '7b', '8a', '8b', '9a', '9b', '10a', '10b', '11a', '11b', '12a', '12b', '13a', '13b', '14a'};
antibodies = {'Rel-A','p-Rel-A','IkBa','p-IkBa','A20'};
x_axis = {'PBS','GOX','TNF','L','M','H'};
colours = {'r','g','b','m','c'};
Y_axis = 'Fold TNF (OD)';

%plot graphs
h = figure('name','Cytoplasm,Averaged,1h');
for i=1:5,
    subplot(5,1,i)
    barwitherr(std_error{2}(i,1:6),averaged_data{2}(i,1:6),'BarWidth',0.7) %note that barwitherr is a downloadable function 
    set(get(gca,'YLabel'), 'String', Y_axis)
    set(gca, 'XTick', 1:6, 'XTickLabel', x_axis,'fontsize',18);  
    title(antibodies(i))

end
h=subplot(5,1,1);
set(h,'ylim',[0,15]);

h=subplot(5,1,3);
set(h,'ylim',[0,4]);

h=subplot(5,1,4);
set(h,'ylim',[0,4]);

h=subplot(5,1,5);
set(h,'ylim',[0,4]);

这很好地生成了我的图表,除了我想更改各个子图的各个条形的颜色。即,您如何将第一个子图的第一个条更改为绿色,但将其余部分保留为蓝色?

干杯

【问题讨论】:

  • 不确定barwitherr() 的工作方式是否相同,但请查看hist 的文档。具体点:“更改直方图颜色属性”。如果您可以使用它,只需使用 if-case 来仅更改您想要的那些图形。
  • No FaceColor 属性不存在此图(无论如何根据错误消息)
  • 如果您在barwitherr() 的末尾输入,'r',它会显示为红色吗?如果是这样,您可以输入一个 if-case 来检查它是否是想要的图形,如果是,则使用修改后的“绘图”。不是一个好的解决方案,但应该可以工作
  • 很遗憾,这没有用 - 错误消息:“属性值对的输入数量不正确”

标签: matlab graph colors


【解决方案1】:

如果我理解正确,请使用this function。

如果是,那么你可以修改barwitherr.m的代码来改变颜色。

  1. 在第 114 行写入后将条的颜色更改为绿色

    set(handles.bar,'FaceColor','g')
    
  2. 将刻度的颜色更改为红色

    % replace line 126 with
    h = errorbar(mean(x,1), values(xOrder,col), lowerErrors(xOrder,col), upperErrors(xOrder, col), '.r');
    hc = get(h, 'Children');
    set(hc(2),'color','r')
    hErrorbar(col) = h;
    
    % after line 131 include
    set(hErrorbar,'color','y')
    hc = get(hErrorbar, 'Children');
    set(hc(2),'color','r')
    
  3. 修改您的 for 循环,其中第一个绘图使用修改后的 barwitherr.m,其他绘图使用原始绘图。
  4. 请参阅Jonathan's answer 了解如何更改各个条形的颜色。如果你在那里替换“colors = [0 1 0; 0 0 1; 0 0 1];” 并将此修改添加到上面的第 1 点,您将得到您想要的。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    我认为解决方案是使用

    [nelements , centers] = hist(Ydata,XData);
    

    获取您感兴趣的数据的 bin 计数,然后对您的数据应用一些阈值,以便只有低于阈值的数据为绿色,其余为蓝色。

    例如:

    CentersBelowTreshold = centers(centers < MinCutoffValue);
    ElementsBelowTreshold = nelements(centers < MinCutoffValue);
    
    CentersAboveTreshold = centers(centers > MinCutoffValue);
    ElementsAboveTreshold = centers(centers > MinCutoffValue;
    

    然后绘制条形图。我想你可以修改 barwitherr 的源代码,因为它基本上使用附加输入参数绘制条形图。

    bar(CentersBelowTreshold,ElementsBelowTreshold,'g')
    
    hold on
    
    bar(CentersAboveTreshold,ElementsAboveTreshold,'b')
    hold off.
    

    【讨论】:

    • 除非有人知道如何取消hggroup 对象的组合。这似乎不太可能。
    • “ungroup hggroup object”是指获取对象的子对象并单独修改它们吗?
    • 有点,我不确定这是否可能,但似乎不太可能。优点是情节可以在创建后进行修改。这在编写报告时很有用,并且该图看起来与您想要的不完全一样。无论如何 +1 为您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 2018-10-28
    • 1970-01-01
    • 2014-08-06
    • 2013-12-11
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多