首页 > 解决方案 > 使用python绘制子图箱线图

问题描述

我想一起绘制两个平行的箱线图。为此,我在 python 中使用了 sub plots 函数,下面是我用于该过程的代码,但我无法从代码中得到很好的输出,因为它已经绘制了两个空图,我如何从输出中删除这些空图? 请为此提供想法?

f, axes = plt.subplots(2,2,figsize = (14,10))
sns.boxplot(x='Heating QC',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,0])
sns.boxplot(x='Heating',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,1])

输出

p

更改低于输出后

IndexError                                Traceback (most recent call last)
<ipython-input-543-7dfa6ebf0390> in <module>
      1 f, axes = plt.subplots(1,2,figsize = (14,10))
----> 2 sns.boxplot(x='Heating QC',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,0])
      3 sns.boxplot(x='Heating',y='SalePrice',hue='Central Air',  data=df ,ax=axes[0,1])

IndexError: too many indices for array

在此处输入图像描述

标签: pythonseabornboxplotsubplotgraph-visualization

解决方案


只需创建两个图,在这种情况下,轴将是 2 个元素的列表并使用这些图。

请参阅文档

f, axes = plt.subplots(2, figsize = (14,10))
sns.boxplot(x='Heating QC',y='SalePrice',hue='Central Air',  data=df, ax=axes[0])
sns.boxplot(x='Heating',y='SalePrice',hue='Central Air',  data=df, ax=axes[1])

推荐阅读