【发布时间】:2021-12-27 15:03:15
【问题描述】:
我遇到了一个问题,即 seaborn 的 relplot 函数创建了一个 FacetGrid,这与手动创建 FacetGrid 不同。我觉得这不直观,并希望 relplot 函数给我一个 FacetGrid,其行为类似于手动创建的。
问题是当对从relplot 返回的FacetGrid 使用map 函数时,它不再考虑指定的色调。这是一个解释我的观点的最小示例:
import numpy as np
import seaborn as sns
import pandas as pd
def foo(color=None, label=None, tag=None):
print(tag, color, label)
x = np.random.randn(100)
df = pd.DataFrame({
'x' : x,
'y' : 2 * x,
'row' : np.random.randn(x.shape[0]) > 0,
'col' : np.random.randn(x.shape[0]) > 0,
'hue' : np.random.randn(x.shape[0]) > 0,
})
g = sns.relplot(data = df, x = 'x', y = 'y', row='row', col='col', hue='hue')
g.map(foo, tag='relplot')
g2 = sns.FacetGrid(data = df, row = 'row', col = 'col', hue='hue')
g2.map(foo, tag='FacetGrid')
在relplot 返回的构面网格上调用map 函数时,它只会被调用四次(每行和每列一次),但不会尊重我还指定了色调这一事实。输出是:
relplot (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) None
relplot (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) None
relplot (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) None
relplot (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) None
如果我将相同的函数映射到手动创建的FacetGrid,它将导致预期的行为:
FacetGrid (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) False
FacetGrid (0.8666666666666667, 0.5176470588235295, 0.3215686274509804) True
FacetGrid (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) False
FacetGrid (0.8666666666666667, 0.5176470588235295, 0.3215686274509804) True
FacetGrid (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) False
FacetGrid (0.8666666666666667, 0.5176470588235295, 0.3215686274509804) True
FacetGrid (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) False
FacetGrid (0.8666666666666667, 0.5176470588235295, 0.3215686274509804) True
是否有任何解释为什么会发生这种情况?有没有办法改变relplot 的行为以匹配预期的行为,即尊重我设置的色调参数?
【问题讨论】:
标签: python seaborn facet-grid