Python 在seaborn中绘制多个箱线图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44552489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:08:11  来源:igfitidea点击:

Plotting multiple boxplots in seaborn?

pythonpandasplotseabornboxplot

提问by Jane Sully

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.

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

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

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 :

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

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

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.

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

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:

我怎么能在seaborn中做到这一点?我知道我可以通过循环路径并使用 boxplot 函数为每个度量单独绘制一个图,如下所示:

sns.boxplot(data=df)   

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

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

回答by Parfait

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

考虑首先为每个相应的数据帧分配一个像Trial这样的分组列,然后是pd.concat您的数据帧,最后pd.melt是指标/值长期数据帧的数据,然后再使用 seaborn 进行绘图。下面用随机数据演示:

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()

enter image description here

在此处输入图片说明

回答by Leandro Cruvinel

# libraries
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from random import randint, gauss

# create a DataFrame
df = pd.DataFrame({
    'Sensations':[randint(1,3) for i in range(300)]
})
df['Temperature'] = df['Sensations'].map(lambda x: gauss(0.8/x,0.1)*40)
df['Sensations'] = df['Sensations'].map({1:'hot',2:'normal',3:'cold'})

# create plot
ax = sns.boxplot(x="Sensations", y="Temperature", data=df)

# show plot
plt.show()

Boxplot example

箱线图示例