在Seaborn中绘制多个箱形图? [英] Plotting multiple boxplots in seaborn?

查看:811
本文介绍了在Seaborn中绘制多个箱形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在大熊猫中使用seaborn绘制箱线图,因为这是一种可视化数据的更好方法,但是我对此不太熟悉.我有三个数据框,它们是不同的指标,并且我想比较不同的指标.我将遍历文件路径以访问它们.

I want to plot boxplots using seaborn in pandas because it is a nicer way to visualize data, but I am not too familiar with it. I have three dataframes that are different metrics, and I want to compare the different metrics. I will loop through the file paths to access them.

for path in paths: 
   df = pd.read_csv(path)

每个指标的dfs是单独的,看起来像这样(其中....表示已填充数据值). 1、2、3、4、5是列名,表示不同的试验:

The dfs for each of the metrics are separate and look something like this (where the .... indicates filled in data values). 1, 2, 3, 4, 5 are the column names and indicate different trials :

    1  2  3  4  5
0   ..............
1   ..............
2   ..............
3   ..............
4   ..............

我想要并排放置试验1、2、3、4、5和3个指标中的每一个的所有图,其中三个指标的所有第一个试验图都在左侧,然后所有第二个试验图将在其右侧,依此类推.

I want to have all the plots for trials 1, 2, 3, 4, 5 and each of the 3 metrics side by side, where all the first trial plots for the three metrics would be on the left, then all the second trial plots would be to the right of that, and so on.

我该如何在Seaborn中做到这一点?我知道我可以通过遍历路径并使用像这样的boxplot函数分别为每个度量绘制图:

How could I go about doing this in seaborn? I know I can do a plot individually for each metric by looping through the path and using the boxplot function like this:

sns.boxplot(data=df)   

但是,我如何能够在同一图上并排拟合其他指标的图?

However, how would I be able to fit the other metrics' plots side-by-side on the same plot?

推荐答案

首先考虑为每个对应的数据帧分配一个分组列,例如 Trial ,然后为pd.concat分配数据帧,最后为pd.melt使用Seaborn进行绘制之前,指标/值长期数据框的数据.下面以随机数据进行演示:

Consider first assigning a grouping column like Trial for each corresponding dataframe, then pd.concat your dataframes, and finally pd.melt the data for a indicator/value long-wise dataframe before plotting with seaborn. Below demonstrates with random data:

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

np.random.seed(44)
# DATAFRAMES WITH TRIAL COLUMN ASSIGNED
df1 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=1)
df2 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=2)
df3 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=3)

cdf = pd.concat([df1, df2, df3])                                # CONCATENATE
mdf = pd.melt(cdf, id_vars=['Trial'], var_name=['Number'])      # MELT

print(mdf.head())
#    Trial Number     value
# 0      1      1 -0.750615
# 1      1      1 -1.715070
# 2      1      1 -0.963404
# 3      1      1  0.360856
# 4      1      1 -1.190504

ax = sns.boxplot(x="Trial", y="value", hue="Number", data=mdf)  # RUN PLOT   
plt.show()

plt.clf()
plt.close()

这篇关于在Seaborn中绘制多个箱形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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