繁体   English   中英

使用 seaborn displot 我无法指定色调来分隔 kde plot 的三个类别

[英]Using seaborn displot I am unable to specify the hue to separate three categories of a kde plot

我正在尝试 plot kernel 密度估计图,用于我从上一个问题中成功融合的数据集。

这就是我应该得到的(这是使用pd.concat([pd.DataFrame[Knn], pd.DataFrame[Kss], pd.DataFrame[Ktt], ...) 在此处输入图像描述

这是 dataframe 的样子:

df_CohBeh
Out[122]: 
    melt         value
0    Knn  2.506430e+07
1    Knn  3.344882e+06
2    Knn  5.783376e+07
3    Knn  1.687218e+06
4    Knn  2.975834e+06
..   ...           ...
106  Ktt  2.056249e+08
107  Ktt  2.085805e+08
108  Ktt  7.791227e+07
109  Ktt  2.072576e+08
110  Ktt  4.658559e+07

[111 rows x 2 columns]

其中熔体列只是定义为指定三个类别的变量。

# In[parameter distribution]

# Melt the results to create a single dataframe
df_CohBeh = pd.melt(df, value_vars=['Knn', 'Kss', 'Ktt'], var_name='melt')

# Normal distribution plots
f, ax = plt.subplots()
sns.set_context("paper", 
                rc={"font.size":12,"axes.titlesize":8,"axes.labelsize":12})

ax = sns.displot(data=df_CohBeh, hue=['Knn', 'Kss', 'Ktt'], 
                 kind="kde", fill=True, legend=False, height=5, aspect=1.6, 
                 cut=0, bw_adjust=1)

ax.set(xlabel='Cohesive Parameters [Pa]', ylabel='Kernel Density Estimation')

# Legend
plt.legend(labels=[r'$K_{nn}$', r'$K_{ss}$', r'$K_{tt}$'], 
           loc='best').set_title("Parameter")

这是我包含hue=['Knn', 'Kss', 'Ktt']时的相关错误消息

ValueError: The following variable cannot be assigned with wide-form data: `hue`

当我从 displot function 调用中删除hue=['Knn', 'Kss', 'Ktt']时,这是生成的 plot。 我不确定错误出在哪里,我得到的绘图不正确。 在此处输入图像描述

任何帮助,将不胜感激。 谢谢!

当您已经拥有长格式数据时,您可以使用x=指定 x 值,并使用hue =指定组或颜色,您只需指定 dataframe 中的列名,例如:

import pandas as pd
import seaborn as sns
import numpy as np

df = pd.DataFrame(np.random.poisson(10,(20,3)),
                  columns=['Knn', 'Kss', 'Ktt'])

df_CohBeh = pd.melt(df, value_vars=['Knn', 'Kss', 'Ktt'], var_name='melt')

sns.displot(data=df_CohBeh, hue='melt',x='value',
            kind="kde", fill=True, legend=False, height=5, aspect=1.6, 
            cut=0, bw_adjust=1)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM