How to Create a Horizontal Bar Chart using Matplotlib

Here is a simple template that you can use to create a horizontal bar chart using Matplotlib:

import matplotlib.pyplot as plt

y_axis = ['Item 1', 'Item 2', 'Item 3', ...]
x_axis = ['Item 1', 'Item 2', 'Item 3', ...]

plt.barh(y_axis,x_axis)
plt.title('title name')
plt.ylabel('y axis name')
plt.xlabel('x axis name')
plt.show()

Later, you’ll also see how to plot a horizontal bar chart with the help of the Pandas library.

Steps to Create a Horizontal Bar Chart using Matplotlib

Step 1: Gather the data for the chart

For example, let’s use the data below to plot the chart:

ProductQuantity
Computer320
Monitor450
Laptop300
Printer120
Tablet280

The above data can be captured in Python using lists:

Product = ['Computer','Monitor','Laptop','Printer','Tablet']
Quantity = [320,450,300,120,280]

print(Product)
print(Quantity)

If you run the code in Python, you’ll get the following lists:

Lists

Step 2: Plot the horizontal bar chart using Matplotlib

You can then plot the chart using this syntax:

import matplotlib.pyplot as plt
   
Product = ['Computer','Monitor','Laptop','Printer','Tablet']
Quantity = [320,450,300,120,280]

plt.barh(Product,Quantity)
plt.title('Store Inventory')
plt.ylabel('Product')
plt.xlabel('Quantity')
plt.show()

Notice that the ‘Product‘ is displayed on the y_axis, while the ‘Quantity‘ is displayed on the x_axis:

How to Create a Horizontal Bar Chart using Matplotlib

Step 3 (optional): Style the chart

You can further style the chart by including the following syntax:

plt.style.use('ggplot')

So the complete code would look as follows:

import matplotlib.pyplot as plt

Product = ['Computer','Monitor','Laptop','Printer','Tablet']
Quantity = [320,450,300,120,280]

plt.style.use('ggplot')

plt.barh(Product,Quantity)
plt.title('Store Inventory')
plt.ylabel('Product')
plt.xlabel('Quantity')
plt.show()

You’ll then get the styled chart:

Style a plot

Plot the Horizontal Bar Chart with the Help of Pandas

You can plot the same bar chart with the help of the Pandas library:

import matplotlib.pyplot as plt
import pandas as pd
   
data = {'Quantity': [320,450,300,120,280]}
df = pd.DataFrame(data,columns=['Quantity'], index = ['Computer','Monitor','Laptop','Printer','Tablet'])

df.plot.barh()

plt.title('Store Inventory')
plt.ylabel('Product')
plt.xlabel('Quantity')
plt.show()

Once you run the code, you’ll get the same bar chart:

How to Create a Horizontal Bar Chart using Matplotlib

Let’s say that you also want to capture the ‘Price‘ (in addition to ‘Quantity’) associated with the product.

In that case, you can use the code below in order to create the horizontal bar chart with both the price and quantity:

import matplotlib.pyplot as plt
import pandas as pd
   
data = {'Quantity': [320,450,300,120,280],
        'Price': [800,250,1200,150,300]
       }
df = pd.DataFrame(data,columns=['Quantity','Price'], index = ['Computer','Monitor','Laptop','Printer','Tablet'])

df.plot.barh()

plt.title('Store Inventory')
plt.ylabel('Product')
plt.xlabel('Quantity')
plt.show()

As you can see, the price is represented in orange, while the quantity represented in blue:

Horizontal Bar Chart using Matplotlib

You can further style the chart using:

import matplotlib.pyplot as plt
import pandas as pd
  
data = {'Quantity': [320,450,300,120,280],
        'Price': [800,250,1200,150,300]
       }
df = pd.DataFrame(data,columns=['Quantity','Price'], index = ['Computer','Monitor','Laptop','Printer','Tablet'])

plt.style.use('ggplot')

df.plot.barh()

plt.title('Store Inventory')
plt.ylabel('Product')
plt.xlabel('Quantity')
plt.show()

Here is the styled chart:
How to Create a Horizontal Bar Chart using Matplotlib

You can learn more about plotting charts by visiting the Matplotlib Documentation.