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