Python 3.x 从seaborn阵列中排列条形图

Python 3.x 从seaborn阵列中排列条形图,python-3.x,for-loop,seaborn,Python 3.x,For Loop,Seaborn,我试图在seaborn中为我的df中“category”列中出现的每个类别绘制一个图。有7个独特的类别。我在一行中成功地做到了这一点,但图太小了。我想把它们分成两排,第一排是4排,第七排是3排。除了应该将子地块的参数更改为2,4之外,我应该如何更改代码 fig, ax = plt.subplots(1, 7) for i,g in enumerate(df.Category.unique()): dfx = df[df['Category'] == g] sns.set(sty

我试图在seaborn中为我的df中“category”列中出现的每个类别绘制一个图。有7个独特的类别。我在一行中成功地做到了这一点,但图太小了。我想把它们分成两排,第一排是4排,第七排是3排。除了应该将子地块的参数更改为2,4之外,我应该如何更改代码

fig, ax = plt.subplots(1, 7)

for i,g in enumerate(df.Category.unique()):
    dfx = df[df['Category'] == g]
    sns.set(style="whitegrid", rc={'figure.figsize':(28,6)})
    sns.barplot(x = dfx['Month'], y = dfx['measure'], ci = None, label = g, ax=ax[i])
    ax[i].legend(loc = 'lower center')

plt.tight_layout()
plt.show()

可以在展平轴阵列上循环,也可以使用groupby简化操作。因此,我认为代码可能看起来像这样未经测试,因为问题中没有提供数据:

sns.set(style="whitegrid")

fig, axes = plt.subplots(2, 4)

for (n, dfx), ax in zip(df.groupby("Category"), axes.flat):
    sns.barplot(x = dfx['Month'], y = dfx['measure'], ci = None, label = n, ax=ax)
    ax.legend(loc = 'lower center')

axes[1,3].axis("off")
plt.tight_layout()
plt.show()

同样,当你使用Seabn时,你可以考虑Seabn.FaseGrand。 这看起来像

sns.set(style="whitegrid")
g = sns.FacetGrid(data=df, col = "Category", col_wrap=4)
g.map(sns.barplot, "Month", "measure")
plt.tight_layout()
plt.show()