I wrote the following code in order to plot a 2x2 displot with seaborn:
df = pd.DataFrame(
{'Re' : x,
'n' : y,
'Type' : tp,
'tg' : tg,
'gt' : gt
})
g = sns.FacetGrid(df, row='gt', col='tg', margin_titles=False, height=2.5, aspect=1.65)
g.map(sns.displot(df, x='Re', y='n', hue='Type', kind='kde',log_scale=True, palette=customPalette, fit_reg=False, x_jitter=.1))
However I get this error that I cannot fix:
func(*plot_args, **plot_kwargs)
TypeError: 'FacetGrid' object is not callable
The x, y, tp, tg and gt imported in the df are lists.
Does anyone have any clue what I might do to fix this? Thank you in advance! :)
*This is how the df looks like: [1]: https://i.sstatic.net/H6J9c.png
Well, sns.displot
is already a FacetGrid
. You can't give it as a parameter to g.map
. Moreover, the parameter to g.map
is meant to be a function without evaluating it (so, without brackets, and the parameters given as parameters to g.map
). See the examples at Seaborn's FacetGrid page.
The most common FacetGrid
parameters (such as row
, col
, height
and aspect
) can be provided directly to sns.displot()
. Less common parameters go into facet_kws=...
.
Here is an example:
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'Re': np.random.rand(100),
'n': np.random.rand(100),
'Type': np.random.choice([*'abc'], 100),
'tg': np.random.choice(['ETG', 'LTG'], 100),
'gt': np.random.randint(0, 2, 100)})
g = sns.displot(df, x='Re', y='n', hue='Type', kind='kde',
row='gt', col='tg', height=2.5, aspect=1.65,
log_scale=True, palette='hls',
facet_kws={'margin_titles': False})
plt.show()
To directly work with FacetGrid
(not recommended), you could create a similar plot with:
g = sns.FacetGrid(df, row='gt', col='tg', hue='Type', palette='hls',
margin_titles=False, height=2.5, aspect=1.65)
g.map_dataframe(sns.kdeplot,
x='Re', y='n',
log_scale=True)
g.add_legend()