在同一图上的Python并排箱图 [英] Python Side-by-side box plots on same figure

查看:829
本文介绍了在同一图上的Python并排箱图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python 2.7中为下面的Pandas数据框中的E列中的每个分类值生成一个箱形图

I am trying to generate a box plot in Python 2.7 for each categorical value in column E from the Pandas dataframe below

          A         B         C         D  E
0  0.647366  0.317832  0.875353  0.993592  1
1  0.504790  0.041806  0.113889  0.445370  2
2  0.769335  0.120647  0.749565  0.935732  3
3  0.215003  0.497402  0.795033  0.246890  1
4  0.841577  0.211128  0.248779  0.250432  1
5  0.045797  0.710889  0.257784  0.207661  4
6  0.229536  0.094308  0.464018  0.402725  3
7  0.067887  0.591637  0.949509  0.858394  2
8  0.827660  0.348025  0.507488  0.343006  3
9  0.559795  0.820231  0.461300  0.921024  1

我愿意使用Matplotlib或任何其他绘图库来执行此操作.到目前为止,以上代码可以将所有类别组合成一个图.这是生成上述数据并生成图的代码:

I would be willing to do this with Matplotlib or any other plotting library. So far the above code can plot all the categories combined on one plot. Here is the code to generate the above data and produce the plot:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()

# Data
df = pd.DataFrame(np.random.rand(10,4),columns=list('ABCD'))
df['E'] = [1,2,3,1,1,4,3,2,3,1]

# Boxplot
bp = ax.boxplot(df.iloc[:,:-1].values, widths=0.2)
plt.show()

在此示例中,类别为1,2,3,4.我想在同一图上并排绘制单独的箱形图,仅针对类别1和2,并在图例中显示类别名称.

In this example, the categories are 1,2,3,4. I would like to plot separate boxplots side-by-side on the same figure, for only categories 1 and 2 and show the category names in the legend.

有没有办法做到这一点?

Is there a way to do this?

其他信息:

输出应类似于此处中的第三个数字-替换是",否"加上"1","2".

The output should look similar to the 3rd figure from here - replace "Yes","No" by "1","2".

推荐答案

从此开始:

import numpy
import pandas
from matplotlib import pyplot
import seaborn
seaborn.set(style="ticks")

# Data
df = pandas.DataFrame(numpy.random.rand(10,4), columns=list('ABCD'))
df['E'] = [1, 2, 3, 1, 1, 4, 3, 2, 3, 1]

您有两种选择.如果单独的轴都可以,

You've got a couple of options. If separate axes are ok,

fig, axes = pyplot.subplots(ncols=4, figsize=(12, 5), sharey=True)
df.query("E in [1, 2]").boxplot(by='E', return_type='axes', ax=axes)

如果您想要1根轴,我认为seaborn会更容易.您只需要清理数据即可.

If you want 1 axes, I think seaborn will be easier. You just need to clean up your data.

ax = (
    df.set_index('E', append=True)  # set E as part of the index
      .stack()                      # pull A - D into rows 
      .to_frame()                   # convert to a dataframe
      .reset_index()                # make the index into reg. columns
      .rename(columns={'level_2': 'quantity', 0: 'value'})  # rename columns
      .drop('level_0', axis='columns')   # drop junk columns
      .pipe((seaborn.boxplot, 'data'), x='E', y='value', hue='quantity', order=[1, 2])  
)
seaborn.despine(trim=True)

关于seaborn的一件很酷的事情是,稍微调整参数可以在情节的布局方面取得很大的成就.如果切换huex变量,则会得到:

The cool thing about seaborn is that tweaking the parameters slightly can achieve a lot in terms of the plot's layout. If we switch our hue and x variables, we get:

ax = (
    df.set_index('E', append=True)  # set E as part of the index
      .stack()                      # pull A - D into rows 
      .to_frame()                   # convert to a dataframe
      .reset_index()                # make the index into reg. columns
      .rename(columns={'level_2': 'quantity', 0: 'value'})  # rename columns
      .drop('level_0', axis='columns')   # drop junk columns
      .pipe((seaborn.boxplot, 'data'), x='quantity', y='value', hue='E', hue_order=[1, 2])  
)
seaborn.despine(trim=True)

如果您感到好奇,则生成的数据框看起来像这样:

If you're curious, the resulting dataframe looks something like this:

    E quantity     value
0   1        A  0.935433
1   1        B  0.862290
2   1        C  0.197243
3   1        D  0.977969
4   2        A  0.675037
5   2        B  0.494440
6   2        C  0.492762
7   2        D  0.531296
8   3        A  0.119273
9   3        B  0.303639
10  3        C  0.911700
11  3        D  0.807861

这篇关于在同一图上的Python并排箱图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆