gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:37 25 4
gpt4 key购买 nike

我的数据框

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))

Box plot

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

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)

boxplot2

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

最佳答案

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

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()

enter image description here

您的第二种方法以空图结束,因为 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()

enter image description here

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

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()

enter image description here

关于python - 索引中按日期排列的多个箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45229177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com