在 python 中堆叠来自不同系列的三个条形图

Stacking three bar charts from different series in python

我是 matplotlib 的新手,不知道如何将多个系列堆叠到一个条形图中。

这是一些数据:

import pandas as pd
import matplotlib.pyplot as plt
###Data
x_lab =  pd.Series(['a','b','c'])
Series1 = pd.Series([25,35,30])
Series2 = pd.Series([40,35,50])
Series3 = pd.Series([35,30,20])

##Creating the 3 bars to plot##
pl_1 = plt.bar(x= x_lab,height = Series1)
pl_2 = plt.bar(x= x_lab,height = Series2)
pl_3 = plt.bar(x= x_lab,height = Series3)

当我运行这段代码时,数据是相互叠加的。我希望数据能够被堆叠起来。

我试过这个:

##First attempt
attempt_1 = plt.bar(x = x_lab, height = [pl_1,pl_2,pl_3], stacked = True)

还有这个:

##Second Attempt 
pl_1 = plt.bar(x= x_lab,height = Series1, stacked = True)
pl_2 = plt.bar(x= x_lab,height = Series2, stacked = True)
pl_3 = plt.bar(x= x_lab,height = Series3, stacked = True)

但都没有用。所需的输出看起来像这样(颜色不需要匹配):

如有任何帮助或指导,我们将不胜感激。

concat + stacked=True

的条形图
import pandas as pd

(pd.concat([Series1, Series2, Series3], axis=1)
     .assign(idx=x_lab).set_index('idx')       # just for the labeling
     .plot.bar(stacked=True))