I am trying to plot kernel density estimation plots for a data set that I successfully melted from a previous question.
This is what I should be getting (This was created using pd.concat([pd.DataFrame[Knn], pd.DataFrame[Kss], pd.DataFrame[Ktt], ...)
:
Here is what the dataframe looks like:
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]
Where the column melt is simply the variable defined to specify three categories.
# 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")
Here is the associated error message when I include hue=['Knn', 'Kss', 'Ktt']
ValueError: The following variable cannot be assigned with wide-form data: `hue`
When I remove hue=['Knn', 'Kss', 'Ktt']
from the displot function call, here is the resulting plot. I'm not sure where the error is that I am getting that isn't plotting correctly.
Any help would be appreciated. Thank you!
When you already have the data in long format, you can specify the x values using x=
and the group or color with hue =
, and you just specify the column name from the dataframe, for example:
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)