Create Horizontal Bar Chart using Matplotlib

Here is a simple template to create a horizontal bar chart using Matplotlib:

import matplotlib.pyplot as plt

y_axis = ["value_1", "value_2", "value_3", ...]
x_axis = ["value_1", "value_2", "value_3", ...]

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

Steps to Create 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

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

Run the code in Python, and you’ll see the ‘product‘ on the y_axis, and the ‘quantity‘ on the x_axis.

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 to style the chart:

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

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, 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.

Let’s say that you also want to capture the ‘price‘ (in addition to the ‘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 the 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, index=["computer", "monitor", "laptop", "printer", "tablet"])

df.plot.barh()

plt.title("store inventory")
plt.ylabel("product")
plt.xlabel("quantity")
plt.show()

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

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

Leave a Comment