【问题标题】:Seaborn and Pandas, grouped box plotSeaborn 和 Pandas,分组箱线图
【发布时间】:2020-08-03 03:42:16
【问题描述】:

总的来说,我对 Python/数据还很陌生,我很难理解这个

我目前有 3 个看起来像这样的数据框

| A | B | C | Type  | 
| 1 | 2 | 6 | Worst |
| 3 | 4 | 5 | Worst | 

| A | B | C | Type   | 
| 1 | 2 | 3 | Medium |

| A | B | C  | Type | 
| 1 | 5 | 20 | Worst|  

我正在尝试创建一个分组箱线图,其中 3 种类型中的每个 A 都有一个箱线图并分组在一起,B 和 C 也是如此

我不确定如何将所有这些数据帧连接在一起,以便将它们发送到 seaborn catplot 函数中。似乎没有太多关于如何设置数据的文档。

【问题讨论】:

标签: python pandas seaborn boxplot


【解决方案1】:

首先将三个数据框与concat 连接起来,然后使用 seaborn 从生成的数据框生成箱线图

import seaborn as sns

# generate the new data frame
df4 = pd.concat([df1, df2, df3])

# Do the boxplot
sns.boxplot(data=df4, x='Type', y='A')

【讨论】:

    【解决方案2】:
    data1 = pd.DataFrame({'A':[1,3], 'B':[2,6], 'C':[6,5] , 'Type':['Worst','Worst']})
    data2 = pd.DataFrame({'A':[1], 'B':[2], 'C':[3], 'Type':['Medium']})
    data3 = pd.DataFrame({'A':[1], 'B':[5], 'C':[20], 'Type':['Worst']})
    df=pd.concat([data1,data2,data3], join='inner')
    plt.figure(figsize= (6,10)
    plt.subplot(3,1,1)
    sns.boxplot(data=df, x ='A', y='Type')
    plt.subplot(3,1,2)
    sns.boxplot(data=df, x ='B', y='Type')
    plt.subplot(3,1,2)
    sns.boxplot(data=df, x ='C', y='Type')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 2016-06-12
      • 2021-03-08
      • 2019-04-29
      • 2021-06-14
      • 2018-02-16
      • 2021-09-08
      • 1970-01-01
      • 2021-02-03
      相关资源
      最近更新 更多