Skip to main content
 首页 » 编程设计

python之索引中按日期排列的多个箱线图

2025年05月04日23wayfarer

我的数据框

index   Dates        Hours_played 
0       2014-11-06   11 
1       2014-12-06   4 
2       2015-09-06   5 
3       2015-97-06   5 

然后,我将日期设置为索引:

             Hours_played 
Dates         
2014-11-06   11 
2014-12-06   4 
2015-09-06   5 
2015-97-06   5 

问题:当我尝试为在索引中找到的每一年创建一个箱形图时,我在同一个网格上得到了两个图。

df.loc['2014']['Hours_played'].plot.box(ylim=(0,200)) 
df.loc['2015']['Hours_played'].plot.box(ylim=(0,200)) 

我尝试了以下方法,但结果是空的:

data_2015 = df.loc['2015']['Hours_played'] 
data_2016 = df.loc['2016']['Hours_played'] 
data_to_plot = [data_2015, data_2016] 
 
mpl_fig = plt.figure() 
ax = mpl_fig.add_subplot(111) 
ax.boxplot(data_to_plot) 
ax.set_ylim(0,300) 

是否可以将它们一个接一个地放在同一个网格中?

请您参考如下方法:

一个简单的解决方案是先按年份分组,然后制作箱线图:

import io 
 
import matplotlib.pyplot as plt 
import pandas as pd 
 
# Re-create your sample data 
s = """Dates,Hours_played 
2014-11-06,11 
2014-12-06,4 
2015-09-06,5 
2015-07-06,5""" 
df = pd.read_table(io.StringIO(s), sep=',', index_col=0, parse_dates=True) 
 
# The following codes are the answer relevant to your question. 
df.groupby(df.index.year).boxplot() 
plt.show() 

您的第二种方法以空图结束,因为 matplotlib 无法正确识别 pandas.DataFrame。尝试使用 Numpy 数组表示:

import io 
 
import matplotlib.pyplot as plt 
import pandas as pd 
 
# Re-create your sample data 
s = """Dates,Hours_played 
2014-11-06,11 
2014-12-06,4 
2015-09-06,5 
2015-07-06,5""" 
df = pd.read_table(io.StringIO(s), sep=',', index_col=0, parse_dates=True) 
 
# The following codes are the answer relevant to your question.     
data_2014 = df[df.index.year == 2014].as_matrix() 
data_2015 = df[df.index.year == 2015].as_matrix() 
data_to_plot = [data_2014, data_2015] 
 
mpl_fig = plt.figure() 
ax = mpl_fig.add_subplot(111) 
ax.boxplot(data_to_plot) 
 
plt.show() 

要使用子图,您需要一个一个地绘制它们:

import io 
 
import matplotlib.pyplot as plt 
import pandas as pd 
 
# Re-create your sample data 
s = """Dates,Hours_played 
2014-11-06,11 
2014-12-06,4 
2015-09-06,5 
2015-07-06,5""" 
df = pd.read_table(io.StringIO(s), sep=',', parse_dates=[0]) 
df['Year'] = df.Dates.dt.year 
df.set_index(['Year', 'Dates'], inplace=True) 
 
# The following codes are the answer relevant to your question. 
mpl_fig = plt.figure() 
ax1 = mpl_fig.add_subplot(121) 
ax1.boxplot(df.loc[2014]['Hours_played'], labels=[2014]) 
ax2 = mpl_fig.add_subplot(122) 
ax2.boxplot(df.loc[2015]['Hours_played'], labels=[2015]) 
 
plt.show()