How to do a barplot using Pandas
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
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.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚