python 中的重叠箱线图
Overlapping boxplots in python
我知道了。数据框:
Av_Temp Tot_Precip
278.001 0
274 0.0751864
270.294 0.631634
271.526 0.229285
272.246 0.0652201
273 0.0840059
270.463 0.0602944
269.983 0.103563
268.774 0.0694555
269.529 0.010908
270.062 0.043915
271.982 0.0295718
并想要绘制一个箱线图,其中 x 轴被 'Av_Temp' 划分为等大小的箱子(在本例中为 2),Y 轴显示 [= 的相应值范围22=]。我有办法。代码(感谢 ),但是,当我绘制箱线图时,它们被绘制在另一个之上。有什么建议吗?
expl_var = 'Av_Temp'
cname = 'Tot_Precip'
df[expl_var+'_Deciles'] = pandas.qcut(df[expl_var], 2)
grp_df = df.groupby(expl_var+'_Deciles').apply(lambda x: numpy.array(x[cname]))
fig, ax = plt.subplots()
for i in range(len(grp_df)):
box_arr = grp_df[i]
box_arr = box_arr[~numpy.isnan(box_arr)]
stats = cbook.boxplot_stats(box_arr, labels = str(i))
ax.bxp(stats)
ax.set_yscale('log')
plt.show()
既然您已经在使用 pandas,为什么不在数据帧上使用箱线图方法呢?
expl_var = 'Av_Temp'
cname = 'Tot_Precip'
df[expl_var+'_Deciles'] = pandas.qcut(df[expl_var], 2)
ax = df.boxplot(by='Av_Temp_Deciles', column='Tot_Precip')
ax.set_yscale('log')
产生这个:http://i.stack.imgur.com/20KPx.png
如果您不喜欢这些标签,请输入
plt.xlabel('');plt.suptitle('');plt.title('')
如果你想要一个标准的箱线图,上面的应该没问题。我对将箱线图分离为 boxplot_stats 和 bxp 的理解是允许您修改或替换生成并提供给绘图例程的统计数据。有关详细信息,请参阅 https://github.com/matplotlib/matplotlib/pull/2643。
如果需要绘制非标准统计数据的箱线图,可以在二维 numpy 数组上使用 boxplot_stats,因此只需调用一次。不需要循环。
expl_var = 'Av_Temp'
cname = 'Tot_Precip'
df[expl_var+'_Deciles'] = pandas.qcut(df[expl_var], 2)
# I moved your nan check into the df apply function
grp_df = df.groupby('Av_Temp_Deciles').apply(lambda x: numpy.array(x[cname][~numpy.isnan(x[cname])]))
# boxplot_stats can take a 2D numpy array of data, and a 1D array of labels
# stats is now a list of dictionaries of stats, one dictionary per quantile
stats = cbook.boxplot_stats(grp_df.values, labels=grp_df.index)
# now it's a one-shot plot, no loops
fig, ax = plt.subplots()
ax.bxp(stats)
ax.set_yscale('log')
我知道了。数据框:
Av_Temp Tot_Precip
278.001 0
274 0.0751864
270.294 0.631634
271.526 0.229285
272.246 0.0652201
273 0.0840059
270.463 0.0602944
269.983 0.103563
268.774 0.0694555
269.529 0.010908
270.062 0.043915
271.982 0.0295718
并想要绘制一个箱线图,其中 x 轴被 'Av_Temp' 划分为等大小的箱子(在本例中为 2),Y 轴显示 [= 的相应值范围22=]。我有办法。代码(感谢
expl_var = 'Av_Temp'
cname = 'Tot_Precip'
df[expl_var+'_Deciles'] = pandas.qcut(df[expl_var], 2)
grp_df = df.groupby(expl_var+'_Deciles').apply(lambda x: numpy.array(x[cname]))
fig, ax = plt.subplots()
for i in range(len(grp_df)):
box_arr = grp_df[i]
box_arr = box_arr[~numpy.isnan(box_arr)]
stats = cbook.boxplot_stats(box_arr, labels = str(i))
ax.bxp(stats)
ax.set_yscale('log')
plt.show()
既然您已经在使用 pandas,为什么不在数据帧上使用箱线图方法呢?
expl_var = 'Av_Temp'
cname = 'Tot_Precip'
df[expl_var+'_Deciles'] = pandas.qcut(df[expl_var], 2)
ax = df.boxplot(by='Av_Temp_Deciles', column='Tot_Precip')
ax.set_yscale('log')
产生这个:http://i.stack.imgur.com/20KPx.png
如果您不喜欢这些标签,请输入
plt.xlabel('');plt.suptitle('');plt.title('')
如果你想要一个标准的箱线图,上面的应该没问题。我对将箱线图分离为 boxplot_stats 和 bxp 的理解是允许您修改或替换生成并提供给绘图例程的统计数据。有关详细信息,请参阅 https://github.com/matplotlib/matplotlib/pull/2643。
如果需要绘制非标准统计数据的箱线图,可以在二维 numpy 数组上使用 boxplot_stats,因此只需调用一次。不需要循环。
expl_var = 'Av_Temp'
cname = 'Tot_Precip'
df[expl_var+'_Deciles'] = pandas.qcut(df[expl_var], 2)
# I moved your nan check into the df apply function
grp_df = df.groupby('Av_Temp_Deciles').apply(lambda x: numpy.array(x[cname][~numpy.isnan(x[cname])]))
# boxplot_stats can take a 2D numpy array of data, and a 1D array of labels
# stats is now a list of dictionaries of stats, one dictionary per quantile
stats = cbook.boxplot_stats(grp_df.values, labels=grp_df.index)
# now it's a one-shot plot, no loops
fig, ax = plt.subplots()
ax.bxp(stats)
ax.set_yscale('log')