[英]Seaborn displot 'FacetGrid' object is not callable
我编写了以下代码,以便使用 seaborn 绘制 2x2 displot:
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))
但是,我收到了无法修复的错误:
func(*plot_args, **plot_kwargs)
TypeError: 'FacetGrid' object is not callable
df 中导入的 x、y、tp、tg 和 gt 是列表。
有谁知道我可以做些什么来解决这个问题? 先感谢您! :)
*这是 df 的样子:[1]: https : //i.stack.imgur.com/H6J9c.png
好吧, sns.displot
已经是一个FacetGrid
。 您不能将其作为参数提供给g.map
。 此外, g.map
的参数意味着是一个不评估它的函数(因此,没有括号,并且参数作为参数提供给g.map
)。 请参阅Seaborn 的 FacetGrid 页面中的示例。
最常见的FacetGrid
参数(例如row
、 col
、 height
和aspect
)可以直接提供给sns.displot()
。 不太常见的参数进入facet_kws=...
。
下面是一个例子:
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()
要直接使用FacetGrid
(不推荐),您可以创建一个类似的图:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.