pandas 如何为 DataFrame 中的每一列绘制箱线图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51777217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:55:06  来源:igfitidea点击:

How to plot a boxplot for each column in a DataFrame?

pythonpandasmatplotlib

提问by helcode

I have a DataFrame dfof multiple columns and I would like to create a boxplotfor each columnusing matplotlib.

我有一个数据帧df多个列的,我想创建一个boxplot为每个列使用matplotlib

df.info()output of my DataFrame below for reference

df.info()下面我的 DataFrame 的输出以供参考

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 9568 entries, 0 to 9567
Data columns (total 5 columns):
Ambient Tempreature    9568 non-null float64
Exhaust Vacuum         9568 non-null float64
Ambient Pressure       9568 non-null float64
Relative Humidity      9568 non-null float64
PE                     9568 non-null float64
dtypes: float64(5)
memory usage: 373.8 KB

回答by helcode

If you want to create a separate plot per column, then you can iterate over each column and use plt.figure()to initiate a new figure for each plot.

如果您想为每列创建一个单独的图,那么您可以遍历每一列并用于plt.figure()为每个图启动一个新图。

import matplotlib.pyplot as plt

for column in df:
    plt.figure()
    df.boxplot([column])

If you want to just put all columns into the same boxplot graph then you can just use df.plot(kind='box')

如果您只想将所有列放入同一个箱线图,那么您可以使用 df.plot(kind='box')