Matlab' FaceColor'忽略输入并选择任意颜色

时间:2017-01-14 10:00:21

标签: matlab plot graph bar-chart

fig = figure('Color','w');
bar(MomVsMarket.Date, [MomVsMarket.MomHold4Weeks ...
    MomVsMarket.MomHold12Weeks  MomVsMarket.MomHold24Weeks],'grouped');
ylabel('Profitability in %');
xlabel('Time');
title('Profitability over Time');
ax = get(gca);
%set the first bar chart style
set(bar,'FaceColor', 'b' ,'BarWidth',2);
%set the second bar chart style
set(bar,'FaceColor','r','BarWidth',2);
%set the third bar chart style
set(bar,'FaceColor','y','BarWidth',2);

生成我的条形图叠加,没有指定的颜色' b',' r'和' 。错误在哪里? enter image description here

1 个答案:

答案 0 :(得分:1)

您应该使用bar的返回值来获取单独条形图的句柄:

function q41648723
%% Generate some data:
d1 = datetime(2006,1,1,0,0,0);
d2 = datetime(2016,7,1,0,0,0);
date = d1:caldays(10):d2;
data = 5*randn(numel(date),3);

MomVsMarket = struct('Date',date,...
                     'MomHold4Weeks', data(:,1),...
                     'MomHold12Weeks',data(:,2),...
                     'MomHold24Weeks',data(:,3));
clear d1 d2 date data;
%% Plot:
figure('Color','w');
hB = bar(MomVsMarket.Date, [MomVsMarket.MomHold4Weeks,...
                            MomVsMarket.MomHold12Weeks,...
                            MomVsMarket.MomHold24Weeks],'grouped');
ylabel('Profitability in %');
xlabel('Time');
title('Profitability over Time');
%set the first bar chart style
set(hB(1),'FaceColor', 'b' ,'BarWidth',2);
%set the second bar chart style
set(hB(2),'FaceColor','r','BarWidth',2);
%set the third bar chart style
set(hB(3),'FaceColor','y','BarWidth',2);

请注意我在上面的代码中如何使用hB。以上结果(MATLAB R2016b):

What the OP wanted, I think...

P.S。

下次,请在您的问题中提供可运行的代码示例(请参阅:Minimal, Complete, and Verifiable example)。如果我不必自己提供一些测试数据,我本可以早点给你一个答案。