簡體   English   中英

如何在python中按字母順序使用顏色漸變的plot標簽?

[英]How to plot labels in alphabetical orders with coulour-gradient in python?

我想 plot 標簽“nutrition_grade_fr”在 python 中按字母順序排列,顏色漸變。

這是我的圖表的代碼,但它沒有按字母順序顯示標簽,也沒有顯示相應的色調:

import seaborn as sns
import matplotlib.pyplot as plt 

df = nutri_app

for elem in ['energy_100g', 'sugars_100g','fat_100g','proteins_100g']:
#Create barplot
    fig, ax = plt.subplots(figsize=(30,6))
    sns.barplot(x="pnns_group1", y=elem, hue="nutrition_grade_fr", data=df)

#Add title
    ax.set_title(f"nutriscore by categories related to {elem} quantity variable")

#Show plot
    plt.show()

您需要pandas.DataFrame.sort_values並選擇一個調色板(例如,藍色):

import seaborn as sns
import matplotlib.pyplot as plt 

df = nutri_app.sort_values(by='nutrition_grade_fr') #ascending=True by default

for elem in ['energy_100g', 'sugars_100g','fat_100g','proteins_100g']:
#Create barplot
    fig, ax = plt.subplots(figsize=(30,6))
    sns.barplot(x="pnns_group1", y=elem, hue="nutrition_grade_fr", data=df, palette='Blues')

#Add title
    ax.set_title(f"nutriscore by categories related to {elem} quantity variable")

#Show plot
plt.show();

暫無
暫無

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

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