【问题标题】:How to set the hue order in Seaborn plots如何在 Seaborn 图中设置色调顺序
【发布时间】:2021-03-16 17:13:01
【问题描述】:

我是 Seaborn 的新手,这可能是一个非常微不足道的问题,但是我正在努力解决这个问题。我有一个名为 titanic 的 Pandas 数据集,我正在使用以下代码绘制 Seaborn 官方文档中描述的条形图:

import seaborn as sns

titanic = sns.load_dataset("titanic")
sns.catplot(x="sex", y="survived", hue="class", kind="bar", data=titanic)

这会产生以下情节:

如您所见,色调由class 表示。问题是:如何手动选择色调顺序,以便可以反转当前的顺序?

【问题讨论】:

    标签: python pandas seaborn bar-chart hue


    【解决方案1】:

    为了手动选择 Seaborn 绘图的色调顺序,您必须将所需的顺序定义为列表,然后将其作为参数 hue_order 传递给绘图函数。以下代码将起作用:

    import seaborn as sns
    
    titanic = sns.load_dataset("titanic")
    hue_order = ['Third', 'Second', 'First']
    sns.catplot(x="sex", y="survived", hue="class", data=titanic, hue_order=hue_order, kind="bar")
    

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 2015-04-12
      • 2014-10-03
      • 1970-01-01
      相关资源
      最近更新 更多