Python Matplotlib按类别和聚合显示子地块

Python Matplotlib按类别和聚合显示子地块,python,pandas,matplotlib,subplot,Python,Pandas,Matplotlib,Subplot,我有一张这样的桌子: data = {'Category':["Toys","Toys","Toys","Toys","Food","Food","Food","Food","Food","Food","Food","Food","Furniture

我有一张这样的桌子:

data = {'Category':["Toys","Toys","Toys","Toys","Food","Food","Food","Food","Food","Food","Food","Food","Furniture","Furniture","Furniture"], 
        'Product':["AA","BB","CC","DD","SSS","DDD","FFF","RRR","EEE","WWW","LLLLL","PPPPPP","LPO","NHY","MKO"],
       'QTY':[100,200,300,50,20,800,300,450,150,320,400,1000,150,900,1150]}
df = pd.DataFrame(data)
df
输出:

因此,我需要制作这样的条形图子图(每个类别的总和乘积):

我的问题是我不知道如何组合类别、系列和聚合。 我设法将它们分为3个子地块(1始终保持空白),但我无法将它们合并

import matplotlib.pyplot as plt

fig, axarr = plt.subplots(2, 2, figsize=(12, 8))

df['Category'].value_counts().plot.bar(
    ax=axarr[0][0], fontsize=12, color='b'
)
axarr[0][0].set_title("Category", fontsize=18)

df['Product'].value_counts().plot.bar(
    ax=axarr[1][0], fontsize=12, color='b'
)
axarr[1][0].set_title("Product", fontsize=18)

df['QTY'].value_counts().plot.bar(
    ax=axarr[1][1], fontsize=12, color='b'
)
axarr[1][1].set_title("QTY", fontsize=18)

plt.subplots_adjust(hspace=.3)
plt.show()
出去


我需要添加什么来组合它们?

使用
seaborn
FaceGrid

import  pandas as pd
import seaborn as sns

data = {'Category':["Toys","Toys","Toys","Toys","Food","Food","Food","Food","Food","Food","Food","Food","Furniture","Furniture","Furniture"], 
        'Product':["AA","BB","CC","DD","SSS","DDD","FFF","RRR","EEE","WWW","LLLLL","PPPPPP","LPO","NHY","MKO"],
       'QTY':[100,200,300,50,20,800,300,450,150,320,400,1000,150,900,1150]}
df = pd.DataFrame(data)

g = sns.FacetGrid(df, col='Category', sharex=False, sharey=False, col_wrap=2, height=3, aspect=1.5)
g.map_dataframe(sns.barplot, x='Product', y='QTY')

谢谢,这是否可以按图表的数量动态设置列数?(col_wrap=2)您可以在那里使用任何您想要的东西,例如,因为有3个类别,所以您可以使用
col_wrap=df['Category'].nunique()
,它将是3-wide或
col_wrap=df['Category'].nunique()-1
使它变为2-wide
import  pandas as pd
import seaborn as sns

data = {'Category':["Toys","Toys","Toys","Toys","Food","Food","Food","Food","Food","Food","Food","Food","Furniture","Furniture","Furniture"], 
        'Product':["AA","BB","CC","DD","SSS","DDD","FFF","RRR","EEE","WWW","LLLLL","PPPPPP","LPO","NHY","MKO"],
       'QTY':[100,200,300,50,20,800,300,450,150,320,400,1000,150,900,1150]}
df = pd.DataFrame(data)

g = sns.FacetGrid(df, col='Category', sharex=False, sharey=False, col_wrap=2, height=3, aspect=1.5)
g.map_dataframe(sns.barplot, x='Product', y='QTY')