简体   繁体   中英

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

I want to plot labels "nutrition_grade_fr" in alphabetical orders with coulour-gradient in python.

Here is the code for my graph but it does not show the labels in alphabetical order nor with the corresponding hue:

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()

You need to pandas.DataFrame.sort_values and choose a color palette (eg, Blues):

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM