首页 > 解决方案 > 位于子图中的多个计数图中的 Seaborn 堆叠条

问题描述

有没有办法将条堆叠起来,countplot所以每个条都包含两种颜色。

到目前为止我的代码:

fig, axes = plt.subplots(4, 4, figsize=(15,13), sharex=True)
axes = axes.flatten()
object_bol = df.dtypes == 'object'
for ax, catplot in zip(axes, df.dtypes[object_bol].index):
    sns.countplot(y=catplot, data=df, ax=ax, hue = "Attrition")

plt.tight_layout()  
plt.show()

下面是我当前的可视化以及我希望实现的堆叠图。

当前可视化

堆积条

标签: pythonseaborn

解决方案


您可以将关键字参数传递plt.barfrom seaborn.countplot

因此,您可以使用该bottom参数。例如(使用plt.bar):

 x = np.arange(0,11,1)
 y = x**2

plt.bar(x, y)
plt.bar(x, y, bottom=y)
plt.xlabel('x')
plt.ylabel('y')

给出:

bar_plot_example


推荐阅读