[英]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.