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

Question:

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()
Asked By: yoopiyo

||

Answers:

You need to pandas.DataFrame.sort_values and choose a color palette (e.g, 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();
Answered By: Timeless