如何根据 python 中的 X 值创建具有自定义宽度的堆叠条形图

how to create stacked bar graph with custom width based on X-values in python

我有一个作业需要生成类似于下图 python 的图表。 此图表由 3 个变量组成。

X 轴工作时间 [h]。

Y 轴堆叠的耗电量 [kW] 和气体 [kg]

是否可以使用 matplotlib 或其他库生成类似于下图的图表? (seaborn, altair, plotnine)

编辑:

是否也可以在图表下方生成一个 table,类似于下面的 table?

最后,我在创建这个问题时解决了我自己的问题:)。 虽然这个答案还是不能在图下面生成一个table.

不过就到此为止吧,希望能帮到同样有同样问题的人。

import matplotlib.pyplot as plt

# Bar's starting x-point 
xPoint = [0, 850, 1100,1220, 1300, 1500]
# Values
Power = [850, 500, 150, 500, 400,1100]
Vapor = [750, 850, 300, 350, 200, 300]

# the width of the bars
width = [850,250,120,80,200,500]
# Bar Color's scheme
color1 = ['#E02E15','#E9C430','#3BB273','#456990','#8491A3','#505168']
color2 = ['#EF6958','#F0D670','#70CF9D','#7598BD','#ABB3BF','#80819D']

fig, ax = plt.subplots()

bar1 = ax.bar(xPoint, height= Power, width=width, align='edge', color=color1)
bar2 = ax.bar(xPoint, height= Vapor, width=width, align='edge', color=color2, bottom=Power)
ax.set_xlabel("Total hour[h]", fontname="MS Gothic", fontsize=16)            # https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text
ax.set_ylabel('Power Consumption+Gas', fontname="MS Gothic", fontsize=16)
ax.set_title('Total Consumption Graph', fontname="MS Gothic", fontsize=20)

ax.bar_label(bar1, label_type='center')
ax.bar_label(bar2, label_type='center')
ax.bar_label(bar2)


plt.show()