【问题标题】:How to disable the nesting made by hue in Seaborn?如何禁用 Seaborn 中的色调嵌套?
【发布时间】:2020-12-01 13:20:10
【问题描述】:

在 Seaborn 的条形图上使用 hue 参数时,它会更改条形的颜色和位置。像下面的例子:

没有色调的绘图:

import seaborn as sns
df = {'x': [1, 2, 3, 4], 'y': [5, 6, 7, 8], 'hue': ['a', 'b', 'b', 'a']}
sns.barplot(
    data = df,
    x = 'x',
    y = 'y',
)

输出: Seaborn barplot without hue

用色调绘制:

df = {'x': [1, 2, 3, 4], 'y': [5, 6, 7, 8], 'hue': ['a', 'b', 'b', 'a']}
sns.barplot(
    data = df,
    x = 'x',
    y = 'y',
    hue = 'hue'
)

输出: Seaborn barplot with hue

那么如何使用默认条形位置和hue 制作的颜色制作绘图?

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    设置dodge=False 为我修复了它。

    import seaborn as sns
    
    df = {'x': [1, 2, 3, 4], 'y': [5, 6, 7, 8], 'hue': ['a', 'b', 'b', 'a']}
    sns.barplot(
        data = df,
        x = 'x',
        y = 'y',
        hue = 'hue',
        dodge=False 
    )
    

    来自docs

    dodge (bool, optional) - 当使用色调嵌套时,元素是否 应该沿着分类轴移动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 2021-03-16
      • 1970-01-01
      • 2016-09-26
      相关资源
      最近更新 更多