In Seaborn, can we set a specific bar order for a barplot?


#1

Question

In the context of this exercise, can we set a specific bar order for a barplot?

Answer

Yes, you can set a specific bar order for a barplot.

To set a specific order for the bars, you can utilize the order parameter when creating a barplot, by passing a list of the bar names in specific order.

When you do this however, if you pass in a name that doesn’t exist, then the bar plotted will become empty, although the bar label will be displayed on the x axis.

Example

sns.barplot(data=df, x="class_name", y="grade", order=["Test 3", "Test 2", "Test 3"])