How to do a barplot using Pandas

1 min

Barplots are one of the most widely used plots in data visualization.

They can be used to compare the evolution of multiple variables over time.

Here is a simple example of a barplot, but this time using the pandas library.

import matplotlib.pyplot as plt
import pandas as pd

# We generate a dataframe with some random data
df = pd.DataFrame(index = ['2018', '2019', '2020', '2021'], 
                  data={"sales": [28429, 41771, 55238, 120681]})

# We plot the data as bar chart.
df.plot(kind="bar", title="Sales per year")

As we can see we are using the DataFrame.plot() method that will do a barplot given the correct DataFrame.

In this example, we are plotting the amount of sales versus the year column.

As we are passing the years as index, it works without specifying the x and the y. Otherwise you would need to pass the columns name as arguments.

Here is the result.