【问题标题】:create a stacked bar chart in matlab在matlab中创建堆积条形图
【发布时间】:2013-04-13 18:53:49
【问题描述】:

您好,我正在尝试在 matlab 中创建堆积条形图,但输出图似乎将第一个数据集 (AD_monthly_generation_250) 添加到第二个数据集 (CC_monthly_demand_2012)。我怎样才能避免这种情况?

我的代码是

%% AD STACKED CC %%

AD_monthly_generation_250 = [186 186 186 186 186 186 186 186 186 186 186 186]';
CC_monthly_demand_2012 = [199.575 206.701 145.284 135.944 127.689 93.281 80.311 78.859 98.145 168.572 206.365 113.030]';


% Create a stacked bar chart using the bar function
figure;
bar(1:12, [ AD_monthly_generation_250 CC_monthly_demand_2012 ], 0.5, 'stack');


% Add title and axis labels
title('Seasonal Anaerobic Digestion (250kWe) Supply to Demand - 2012','FontSize',22);
 set(gca,'XTickLabel',{'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',' Sep',     'Oct', 'Nov',' Dec'},'FontSize',18)
 ylabel('Energy (MWh)');

【问题讨论】:

    标签: matlab plot bar-chart stackedbarseries


    【解决方案1】:

    我认为'stack' 选项按预期工作......试试这个代码,也许这是你想要的:

    %% AD STACKED CC %%
    
    AD_monthly_generation_250 = [186 186 186 186 186 186 186 186 186 186 186 186]';
    CC_monthly_demand_2012 = [199.575 206.701 145.284 135.944 127.689 93.281 80.311 78.859 98.145 168.572 206.365 113.030]';
    
    % Create a stacked bar chart using the bar function
    figure;
    % bar(1:12, [ AD_monthly_generation_250 CC_monthly_demand_2012 ], 0.5, 'stack');
    bar(1:12, AD_monthly_generation_250, 0.5, 'b');
    hold on;
    bar(1:12, CC_monthly_demand_2012, 0.5, 'r');
    hold off;
    
    % Add title and axis labels
    title('Seasonal Anaerobic Digestion (250kWe) Supply to Demand - 2012','FontSize',22);
     set(gca,'XTickLabel',{'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',' Sep',     'Oct', 'Nov',' Dec'},'FontSize',18)
     ylabel('Energy (MWh)');
    

    【讨论】:

    • 谢谢,但这似乎不起作用。我得到的只是一个条形图,其 y 轴值为 AD_monthly_generation_250
    • @user643469:我错误地评论了其中一行。我更新了上述答案中的代码。请再试一次好吗?
    猜你喜欢
    • 2011-09-18
    • 1970-01-01
    • 2019-04-10
    • 2017-10-13
    • 2019-08-27
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多