【问题标题】:FacetGrid returned seaborn's relplot does not respect hueFacetGrid 返回 seaborn 的 relplot 不尊重色调
【发布时间】: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


    【解决方案1】:

    有什么解释为什么会这样吗?

    是的,在relplot 中,hue 逻辑都在scatterplot 中处理,而不是由FacetGrid 处理。

    有没有办法改变 relplot 的行为以匹配预期的行为?

    不,在这种情况下,您需要让自定义函数在内部处理色调逻辑,或者直接以 FacetGrid 开头。

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2016-08-01
      • 2019-02-02
      • 2021-12-22
      • 2020-09-22
      • 2021-12-29
      • 2020-05-03
      • 1970-01-01
      • 2019-01-02
      相关资源
      最近更新 更多