【问题标题】:plot a stacked bar chart in matlab that shows all the values在 matlab 中绘制一个显示所有值的堆积条形图
【发布时间】:2013-04-27 13:16:06
【问题描述】:

您好,我在 Matlab 中有以下代码

CC_monthly_thermal_demand = [495 500 500 195 210 100 70 65 85 265 320 430]';
AD_monthly_thermal_generation_250 = [193 193 193 193 193 193 193 193 193 193 193 193]';
figure;
bar(1:12,AD_monthly_thermal_generation_250)
hold on
bar(1:12,CC_monthly_thermal_demand,'r')
set(gca,'XTickLabel',{'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',' Sep', 'Oct', 'Nov',' Dec'},'FontSize',18)
title('Seasonal Anaerobic Digestion (250kWe) Thermal Energy Supply to Demand - 2012','FontSize',22)
ylabel('Thermal Energy (MWhe)')
legend('250 kWth Supply','Thermal Energy Demand')
grid on
axis([0 13 0 600])

我正在尝试绘制一个堆积条形图,显示每个条形的每个变量的颜色。但是,对于“AD_monthly_thermal_generation_250”值低于“CC_monthly_thermal_demand”的条形图,“AD_monthly_thermal_generation_250”颜色完全被“CC_monthly_thermal_demand”覆盖,因此我看不到这些值。有没有可能看到他们?

谢谢

【问题讨论】:

    标签: matlab bar-chart stackedbarseries


    【解决方案1】:

    将输入组合在一起,使每个系列都是一列,然后使用选项'stack'

    A = [495 500 500 195 210 100 70 65 85 265 320 430]';
    B = [193 193 193 193 20  193 193 193 193 193 193 193]';
    bar([B,A],'stacked')
    

    编辑解决评论:

    % Create random data and filler
    A      = randi(100,10,1);
    B      = randi(100,10,1);
    mxA    = max(A);
    filler = mxA-A;
    
    % Plot stacked bar chart
    h = bar([A,filler,B],'stacked');
    
    % Set the filler to transparent
    set(h(2),'Face','none','Edge','none')
    
    % Set y-labels
    yticks = linspace(0,max(B),5) + mxA;
    set(gca,'Ytick', [linspace(0,mxA,5) yticks(2:end)],'YtickL', [linspace(0,mxA,5) yticks(2:end)-mxA])
    

    【讨论】:

    • 是否可以使条形图的y值与“A”和“B”中的数字相对应。我认为您的建议确实显示了每个条的两个值,但这些值并不代表“A”和“B”中的值。它的作用是在“B”之后开始计算“A”,所以你得到了条形图的顶部 = A+B
    • 感谢您的建议 - 看起来不错。如果我的要求是不可能的,请告诉我(在一张图表上显示条形图)。
    • 您的意思是将条形分组吗?喜欢在 bar([A,B]) 中?
    • 这不是每 x 刻度给出两个柱吗?
    • 请花点时间阅读函数的文档(查看示例),然后回来准确地询问您想要实现的目标。
    猜你喜欢
    • 1970-01-01
    • 2020-05-27
    • 2018-11-30
    • 2012-09-17
    • 2021-12-27
    • 2021-10-07
    • 1970-01-01
    • 2019-04-24
    相关资源
    最近更新 更多