Libraries & Dataset
First, we need to load a few libraries and a dataset:
📍 Most basic
Building a horizontal barplot with matplotlib follows pretty much the same process as a vertical barplot. The only difference is that the barh() function must be used instead of the bar() function. Here is a basic example:
↕️ Control order
A bar chart is always more insightful when ordered. It allows to understand how items are ranked much quicker. Since matplotlib traces the values in the order in which they are found in the data frame, you just have to sort the dataframe upfront to sort the chart:
✨ Object-oriented API
Same graph as above using the matplotlib object oriented API instead of the pyplot API. See the matplotlib section for more about this.
Going further
This post explains how to create a horizontal barplot using matplotlib!
For more examples of how to create or customize your barplots, see the barplot section. You may also be interested in how to custom colors in a barplot.