簡體   English   中英

每個類別seaborn的不同色調

[英]Different hue for each category seaborn

所以,我用最簡單的方法用 seaborn 制作了一個帶狀圖,有 5 個不同的類別:

sns.set_style('whitegrid')
plt.figure(figsize=(35,20))
sns.set(font_scale = 3)
sns.stripplot(df.speed, df.routeID, hue=df.speed>50, jitter=0.2, alpha=0.5, size=10, edgecolor='black')
plt.xlabel("Speed", size=40)
plt.ylabel("route ID", size=40)
plt.title("Velocity stripplot", size=50)

現在,問題是我想為每個類別設置不同的色調,比如說第一類的速度大於 50 公里小時,第二類的速度大於 30 公里,依此類推。 這可能嗎? 我試圖通過一個色調列表來做到這一點:

hue=([("ROUTE 30">50),("ROUTE 104">0)])

但它標記: SyntaxError: invalid syntax
問題是,我想在同一個情節中一次性完成(因為最明顯的答案是單獨繪制),這怎么做?

編輯:我遵循了建議的答案。 使用相同的代碼:

plt.figure(figsize=(20,7))

my_palette = ['b' if x > 82 else 'g' for x in df.speed.values]

sns.stripplot(df.speed, df.routeID, jitter=0.2, alpha=0.5, size=8, edgecolor='black', palette = my_palette)

但結果並不像預期的那樣:

在此處輸入圖片說明

我不明白這里有什么問題。 有任何想法嗎?

我建議在df為點顏色創建單獨的列。 嘗試這個:

# INITIAL DATA
n = 1000
df = pd.DataFrame()
df['speed'] = np.random.randint(10,90,n)
df['routeID'] = np.random.choice(['ROUTE_5','ROUTE_66','ROUTE_95','ROUTE_101'], n)

# set hue indices to match your conditions
df['hue'] = 'normal'  # new column with default value
df.loc[df.speed > 50, 'hue'] = 'fast'
df.loc[(df.routeID=="ROUTE_5") & (df.speed>40)|
       (df.routeID=="ROUTE_66") & (df.speed>30)|
       (df.routeID=="ROUTE_95") & (df.speed>60),
       'hue'] = 'special'
palette = {'normal':'g','fast':'r','special':'magenta'} 

sns.stripplot(x=df.speed, y=df.routeID, size=15,
    hue=df.hue, palette=palette)

在此處輸入圖片說明

暫無
暫無

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

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