未能使用 Seaborn 对 pandas 中的 groupby 数据绘制每个 canvas 中的一个图形

failed to boxplot one figure in each canvas using Seaborn for groupby data in pandas

我有一个数据集,如下:

funding_round_type  variable    value
venture raised_amount_usd   7.0000000000
seed    raised_amount_usd   5.8450980400
venture raised_amount_usd   6.5323565818
venture raised_amount_usd   6.3010299957
seed    raised_amount_usd   4.6370892735
venture raised_amount_usd   5.8570253658
seed    raised_amount_usd   6.4771212547
venture raised_amount_usd   7.3010299957
venture raised_amount_usd   6.2304489214
venture raised_amount_usd   6.9493900066
seed    raised_amount_usd   5.1760912591
seed    raised_amount_usd   6.0000217142
seed    raised_amount_usd   4.6020599913
private_equity  raised_amount_usd   6.7781512504
venture raised_amount_usd   6.6989700043
venture raised_amount_usd   6.6020599913
seed    raised_amount_usd   5.6020599913
seed    raised_amount_usd   4.6020599913
seed    raised_amount_usd   5.6989700043
angel   raised_amount_usd   7.6427005481
venture raised_amount_usd   6.4014005408
seed    raised_amount_usd   4.1760912591
venture raised_amount_usd   5.4247836644
venture raised_amount_usd   6.4634199325
venture raised_amount_usd   6.1760912591
venture raised_amount_usd   5.8075350281
seed    raised_amount_usd   5.5910646070
venture raised_amount_usd   6.8197211705
venture raised_amount_usd   6.2397773156
seed    raised_amount_usd   5.1211296041
venture raised_amount_usd   6.2552725051
venture raised_amount_usd   6.9493900066
venture raised_amount_usd   7.0000000000
venture raised_amount_usd   6.4771212547
venture raised_amount_usd   6.4771212547
venture raised_amount_usd   6.1760912591
seed    raised_amount_usd   5.6720978579
venture raised_amount_usd   6.5477747054
seed    raised_amount_usd   5.8750612634
venture raised_amount_usd   5.6989700043
venture raised_amount_usd   7.5440680444
seed    raised_amount_usd   4.6989700043
seed    raised_amount_usd   5.0000000000
seed    raised_amount_usd   6.2041199827
angel   raised_amount_usd   5.0000000000
venture raised_amount_usd   7.7403626895
venture raised_amount_usd   5.7403626895
seed    raised_amount_usd   6.2920142616
venture raised_amount_usd   7.3010299957
venture raised_amount_usd   6.2552725051
seed    raised_amount_usd   5.9708116109
seed    raised_amount_usd   5.3979400087
seed    raised_amount_usd   5.2304489214
seed    raised_amount_usd   4.1461280357
angel   raised_amount_usd   5.6989700043
seed    raised_amount_usd   4.7403626895
venture raised_amount_usd   5.1768636160
seed    raised_amount_usd   5.6989700043
venture raised_amount_usd   6.0000000000
venture raised_amount_usd   7.1139433523
venture raised_amount_usd   6.7323937598
seed    raised_amount_usd   5.5784237281
venture raised_amount_usd   7.0791576125
seed    raised_amount_usd   5.1160628741
venture raised_amount_usd   6.2574520108
angel   raised_amount_usd   5.7920371044
venture raised_amount_usd   6.3802112417
venture raised_amount_usd   6.3979400087
venture raised_amount_usd   7.1760912591
venture raised_amount_usd   6.6989700043
venture raised_amount_usd   6.7118072290
venture raised_amount_usd   7.0934216852
venture raised_amount_usd   6.7781512504
venture raised_amount_usd   7.3010299957
seed    raised_amount_usd   5.8450980400
venture raised_amount_usd   6.4771212547
venture raised_amount_usd   6.3979400087
seed    raised_amount_usd   5.6989700043
seed    raised_amount_usd   6.3424226808
angel   raised_amount_usd   6.0000000000
angel   raised_amount_usd   5.3010299957
seed    raised_amount_usd   5.3010299957
venture raised_amount_usd   5.3009366123
angel   raised_amount_usd   4.6989700043
venture raised_amount_usd   7.2041199827
venture raised_amount_usd   7.0000000000
venture raised_amount_usd   7.1461280357
venture raised_amount_usd   6.6020599913
seed    raised_amount_usd   6.0969100130
venture raised_amount_usd   6.2011192509
venture raised_amount_usd   5.6803482066
venture raised_amount_usd   7.9157813085
venture raised_amount_usd   6.6989700043
venture raised_amount_usd   7.0000000000
venture raised_amount_usd   7.3010299957
venture raised_amount_usd   8.0000000000
venture raised_amount_usd   6.8450980400
venture raised_amount_usd   7.6020599913
venture raised_amount_usd   6.7944880467
venture raised_amount_usd   6.6989700043
private_equity  raised_amount_usd   8.3010299957
angel   raised_amount_usd   4.6020599913
venture raised_amount_usd   6.3979400087
seed    raised_amount_usd   4.6020599913
seed    raised_amount_usd   4.6989700043
seed    raised_amount_usd   6.2304489214
venture raised_amount_usd   6.1613680022
seed    raised_amount_usd   5.9542425094
seed    raised_amount_usd   5.3010299957
seed    raised_amount_usd   6.0000000000
venture raised_amount_usd   5.2125391607
venture raised_amount_usd   6.6020599913
seed    raised_amount_usd   5.3222192947
seed    raised_amount_usd   3.3010299957
venture raised_amount_usd   7.4771212547
seed    raised_amount_usd   5.6989700043
angel   raised_amount_usd   5.3455540773
seed    raised_amount_usd   4.9665265584
angel   raised_amount_usd   5.6503667852
venture raised_amount_usd   7.1760912591
seed    raised_amount_usd   4.3010299957
seed    raised_amount_usd   5.1335389084
venture raised_amount_usd   4.4771212547
venture raised_amount_usd   7.1760912591
venture raised_amount_usd   7.6232492904
angel   raised_amount_usd   5.8750612634
venture raised_amount_usd   6.3979400087
venture raised_amount_usd   5.6989700043
venture raised_amount_usd   6.0000000000
venture raised_amount_usd   6.2201080880
seed    raised_amount_usd   4.7634279936
seed    raised_amount_usd   5.3100323422
venture raised_amount_usd   6.3089940138
venture raised_amount_usd   5.1760912591
venture raised_amount_usd   6.3341345481
venture raised_amount_usd   7.4734869701
venture raised_amount_usd   6.9030899870
venture raised_amount_usd   7.4149733480
venture raised_amount_usd   6.3979400087
venture raised_amount_usd   6.8573324964
seed    raised_amount_usd   5.0599570200
seed    raised_amount_usd   5.5073835557
seed    raised_amount_usd   5.9030899870
seed    raised_amount_usd   5.0791812460
seed    raised_amount_usd   6.3222192947
venture raised_amount_usd   6.3010299957
venture raised_amount_usd   5.9030899870
seed    raised_amount_usd   4.6989700043
angel   raised_amount_usd   6.1655790462
venture raised_amount_usd   6.8041058192
private_equity  raised_amount_usd   8.5118833610
private_equity  raised_amount_usd   5.3025646833
private_equity  raised_amount_usd   7.9294189257
private_equity  raised_amount_usd   7.6989700043
private_equity  raised_amount_usd   6.6232492904
private_equity  raised_amount_usd   8.0000000000
private_equity  raised_amount_usd   6.5287849786
private_equity  raised_amount_usd   6.8450980400
private_equity  raised_amount_usd   5.5865648018
private_equity  raised_amount_usd   6.4771212547
private_equity  raised_amount_usd   7.8750612634
private_equity  raised_amount_usd   8.0000000000
private_equity  raised_amount_usd   8.0413926852
private_equity  raised_amount_usd   8.0638509231
private_equity  raised_amount_usd   7.9030899870
private_equity  raised_amount_usd   7.3010299957
private_equity  raised_amount_usd   7.3010299957
private_equity  raised_amount_usd   6.5326671293
private_equity  raised_amount_usd   7.8864887680
private_equity  raised_amount_usd   8.7403626895
private_equity  raised_amount_usd   8.0413926852
private_equity  raised_amount_usd   8.6253124510
private_equity  raised_amount_usd   8.3521825181
private_equity  raised_amount_usd   7.9364383376
private_equity  raised_amount_usd   7.6020599913
private_equity  raised_amount_usd   7.6532125138

我想在不同的 canvas(2 x 2 帧,即 2 行和 2 列)中为每个 funding_round_type 绘制箱线图。

我试图为此目的使用 seaborn 包,我的代码看起来像这样:

 fig, axs = plt.subplots(ncols=4)
 df.groupby('funding_round_type')['raised_amount_usd'].plot.box(subplots=True)
 plt.show()

它没有产生我正在寻找的结果。

那我也试了:

 sns.regplot(x='funding_round_type', y='raised_amount_usd', data=df, ax=axs[0])
 sns.regplot(x='funding_round_type', y='raised_amount_usd', data=df, ax=axs[1])
 sns.boxplot(x='funding_round_type', y='raised_amount_usd', data=df, ax=axs[2])
 sns.boxplot(x='funding_round_type', y='raised_amount_usd', data=df, ax=axs[3])

这给了我一个错误:

    ....
    .......
    2902         if tolerance is not None:
    KeyError: 'raised_amount_usd'

如何在 canvas 的每一帧中为每个 funding_round_type 获取单个箱线图?

你试过了吗pandas.DataFrame.boxplot

这看起来像您要求的结果。

df.groupby('funding_round_type').boxplot(subplots=True, figsize=(6,6))

输出

有很多方法可以为每个子图创建箱线图。

下面的代码假定您希望每个方框有不同的 x 范围(否则您可以简单地将它们放在一个图中)。循环遍历每个轴,示例代码将 x 轴限制在 5% 和 95% 之间的间隔,因此异常值被切除。

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

df = pd.DataFrame({"funding_round_type": np.random.choice(["seed", "venture", "angel", "private_equity"], 50),
                   "value": np.random.normal(50, 10, 50)})

g = sns.FacetGrid(df, col="funding_round_type", col_wrap=2, sharex=False)
g.map(sns.boxplot, "value")
for col, ax in zip(g.col_names, g.axes.flat):
    ax.set_xlim(df[df["funding_round_type"] == col]["value"].quantile([0.05, 0.95]))
plt.tight_layout()
plt.show()

要获得垂直框,一个问题是这些函数需要一个x参数。一个技巧是添加一个空名称且充满空字符串的虚拟列。这是一个使用 catplot:

的例子
df[""] = "" # create a dummy column to serve as x
g = sns.catplot(data=df, x="", y="value", col="funding_round_type", kind="box", col_wrap=2, sharey=False)
for col, ax in zip(g.col_names, g.axes.flat):
    ax.set_ylim(df[df["funding_round_type"] == col]["value"].quantile([0.05, 0.95]))
plt.tight_layout()
plt.show()