在子图中使用 Pandas df.boxplot()
Using Pandas df.boxplot() in subplots
我正在尝试在 pandas 数据框中创建按其他列分组的列的子图。在这里,我创建并遍历子图,并尝试为每个子图添加一个箱线图。
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()。
您应该将 axes
作为参数传递给 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()
我正在尝试在 pandas 数据框中创建按其他列分组的列的子图。在这里,我创建并遍历子图,并尝试为每个子图添加一个箱线图。
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()。
您应该将 axes
作为参数传递给 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()