簡體   English   中英

如何在 Seaborn 圖中設置色調順序

[英]How to set the hue order in Seaborn plots

我是 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)

這將產生以下 plot:

在此處輸入圖像描述

如您所見,色調由class表示。 問題是:如何手動選擇色調順序,以便我可以反轉當前的順序?

In order to manually select the hue order of a Seaborn plot, you have to define the desired order as a list and then pass it to the plot function as the argument 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM