简体   繁体   English

用熊猫数据框绘制箱线图

[英]Plotting boxplot with pandas dataframe

I've created a pandas dataframe of PM2.5 data and want to create individual boxplots for each site with box and whisker plots for each year within this (with the top and bottom 10th percentiles).我已经创建了一个 PM2.5 数据的 pandas 数据框,并希望为每个站点创建单独的箱线图,其中包含每年的箱线图和胡须图(顶部和底部的第 10 个百分位数)。 How do I go about this?我该怎么做?


Date            Aberdeen  Auchencorth Moss  Belfast Centre  Birmingham Tyburn  

2000-01-01        NaN               NaN             NaN                NaN   
2000-01-02        NaN               NaN             NaN                NaN   
2000-01-03        NaN               NaN             NaN                NaN   
2000-01-04        NaN               NaN             NaN                NaN   
2000-01-05        NaN               NaN             NaN                NaN   
2000-01-06        NaN               NaN             NaN                NaN   
2000-01-07        NaN               NaN             NaN                NaN   
2000-01-08        NaN               NaN             NaN                NaN   
2000-01-09        NaN               NaN             NaN                NaN   
2000-01-10        NaN               NaN             NaN                NaN   
2000-01-11        NaN               NaN             NaN                NaN   
2000-01-12        NaN               NaN             NaN                NaN   
2000-01-13        NaN               NaN             NaN                NaN   
2000-01-14        NaN               NaN             NaN                NaN   
2000-01-15        NaN               NaN             NaN                NaN   
2000-01-16        NaN               NaN             NaN                NaN   
2000-01-17        NaN               NaN             NaN                NaN   
2000-01-18        NaN               NaN             NaN                NaN   
2000-01-19        NaN               NaN             NaN                NaN   
2000-01-20        NaN               NaN             NaN                NaN   
2000-01-21        NaN               NaN             NaN                NaN   
2000-01-22        NaN               NaN             NaN                NaN   
2000-01-23        NaN               NaN             NaN                NaN   
2000-01-24        NaN               NaN             NaN                NaN   
2000-01-25        NaN               NaN             NaN                NaN   
2000-01-26        NaN               NaN             NaN                NaN   
2000-01-27        NaN               NaN             NaN                NaN   
2000-01-28        NaN               NaN             NaN                NaN   
2000-01-29        NaN               NaN             NaN                NaN   
2000-01-30        NaN               NaN             NaN                NaN   
              ...               ...             ...                ...   
2017-04-02        3.0               4.0             7.0               10.0   
2017-04-03        5.0               4.0             9.0               14.0   
2017-04-04        3.0               5.0             8.0                9.0   
2017-04-05        7.0               5.0             7.0                7.0   
2017-04-06        3.0               3.0             7.0               10.0   
2017-04-07        3.0               3.0            11.0               14.0   
2017-04-08       11.0              12.0            20.0               26.0   
2017-04-09       11.0              15.0            17.0               25.0   
2017-04-10        3.0               4.0             8.0                5.0   
2017-04-11        1.0               6.0             9.0                7.0   
2017-04-12        2.0               4.0             5.0                6.0   
2017-04-13        2.0               3.0             6.0                6.0   
2017-04-14        2.0               3.0             6.0                6.0   
2017-04-15        3.0               3.0             6.0                6.0   
2017-04-16        3.0               3.0             5.0                5.0   
2017-04-17        4.0               3.0             7.0               11.0   
2017-04-18        4.0               3.0             7.0                7.0   
2017-04-19        6.0               4.0            11.0               13.0   
2017-04-20        3.0               4.0            12.0               12.0   
2017-04-21        3.0               4.0            11.0               11.0   
2017-04-22        3.0               4.0             9.0                8.0   
2017-04-23        3.0               4.0             6.0                9.0   
2017-04-24        3.0               2.0             4.0                6.0   
2017-04-25        3.0               3.0             6.0                5.0   
2017-04-26        3.0               3.0             6.0                6.0   
2017-04-27        3.0               2.0             6.0                8.0   
2017-04-28        NaN               3.0             8.0                8.0   
2017-04-29        NaN               6.0             7.0                9.0   
2017-04-30        NaN              17.0            20.0               19.0   
2017-05-01       19.0              18.0            20.0                8.0

Here's a go.开始吧。

import matplotlib.pyplot as plt
import pandas as pd

# copy your sample
df = pd.read_clipboard(header=0, index_col='Date').fillna(0)

# remove row with '...'
df = df[df.Moss != '...'].astype(float)

# set index to datetime index
df.index = pd.DatetimeIndex(df.index)

# groupby year
grouped = dict(list(df.groupby(date_index.year)))

# set up a figure with 1 row, 2 colums
fig, ax = plt.subplots(1, 2,
                       sharey=True, 
                       figsize=(8, 6), 
                       tight_layout=True)

# iterate through our grouped and plot 
for i, (k, v) in enumerate(grouped.items()):
    v.boxplot(ax=ax[i],
              rot=90, 
              figsize=(3, 3)).set(title=f'{k}')

plt.show()

在此处输入图像描述

Year 2000 has no data points, but you can see the gist of it. 2000没有数据点,但您可以看出其中的要点。

You could try using matplotlib你可以尝试使用 matplotlib

import matplotlib.pyplot as plt
plt.boxplot(your_df['Aberdeen'].values)
plt.show()

If you are using jupyter use %matplotlib inline如果您使用的是 jupyter,请使用%matplotlib inline

import pandas as pd
df = pd.DataFrame({'Date': ['2000-10-10', '2000-09-20', '2001-09-23', '2001-09-10', '2001-09-02', '2002-04-29',],
          'Aberdeen': [9,5,1, 2, 6, 1]})

df['Year'] = df['Date'].str.split('-')
df['Year'] = df['Year'].apply(lambda x : x[0])
sns.boxplot(df['Year'], df['Aberdeen'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM