Python 3.x 在子地块中使用df.boxplot()

Python 3.x 在子地块中使用df.boxplot(),python-3.x,pandas,boxplot,subplot,Python 3.x,Pandas,Boxplot,Subplot,我试图在pandas dataframe中创建一个列的子批,该列按其他列分组。在这里,我创建并迭代子图,并尝试为每个子图添加一个箱线图 fig, axes = plt.subplots(nrows=2, ncols=2) # create 2x2 array of subplots axes[0,0] = df.boxplot(column='price') # add boxplot to 1st subplot axes[0,1] = df.boxplot(column='price',

我试图在pandas dataframe中创建一个列的子批,该列按其他列分组。在这里,我创建并迭代子图,并尝试为每个子图添加一个箱线图

fig, axes = plt.subplots(nrows=2, ncols=2) # create 2x2 array of subplots

axes[0,0] = df.boxplot(column='price') # add boxplot to 1st subplot
axes[0,1] = df.boxplot(column='price', by='bedrooms') # add boxplot to 2nd subplot
# etc.
plt.show()
这导致

如您所见,箱线图没有添加到子图中。我不确定我会错在哪里。我找到的所有文档都说[0,0]位于左上角,箱线图可以工作。。
我需要特别使用df.boxplots()。

您应该将
轴作为参数传递给plot函数:

fig, axes = plt.subplots(nrows=2, ncols=2) # create 2x2 array of subplots

df.boxplot(column='price', ax=axes[0,0]) # add boxplot to 1st subplot
df.boxplot(column='price', by='bedrooms', ax=axes[0,1]) # add boxplot to 2nd subplot
# etc.
plt.show()