Dav*_*ide 1 python statistics matplotlib boxplot pandas
我正在尝试使用此 python 循环打印数据集中每个变量的所有箱线图。
colNameList = list(df.columns)
for i in range (0, len(df.columns)):
df.boxplot(column=colNameList[i])
Run Code Online (Sandbox Code Playgroud)
其中 df 是我的数据集。
为什么这个简单的代码只向我显示最后一个箱线图?
IIUC,您希望每列都有一个框,这是df.boxplot().
示例数据框:
df = pd.DataFrame({'col1':np.random.randint(0,9,100),
'col2':np.random.randint(2,12,100),
'col3':np.random.randint(4,14,100)})
>>> df.head()
col1 col2 col3
0 6 9 4
1 5 2 8
2 0 7 11
3 0 10 9
4 0 3 8
Run Code Online (Sandbox Code Playgroud)
绘图:
df.boxplot()
Run Code Online (Sandbox Code Playgroud)
如果您只想要某些列:
df[['col1', 'col2']].boxplot()
# or
df.boxplot(column=['col1', 'col2'])
Run Code Online (Sandbox Code Playgroud)
编辑根据您的评论,这里有一种方法可以将每个单独的框另存为单独的箱线图,以便您可以单独查看它们。
for i in df.columns:
df.boxplot(column=i)
plt.savefig('plot'+str(i)+'.png')
plt.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5492 次 |
| 最近记录: |