How to bar plot in Python

1 min readMatplotlibPlotDataFrame
7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Plotting is one of the fundamental tool of the modern Data Scientist.

Simple plots are made easy using the .plot()  from the Pandas library

You will also need to use the matplotlib library

Installing Pandas and Matplotlib

pip install pandas matplotlib
Installing the libraries using python 2.7
pip3 install pandas matplotlib
Installing the libraries using Python 3.x

Using Pandas

Pandas do have a little charting library in his toolbox. But will require matplotlib if you want to show it if you are running it from a script.

Here is the simplest example :

# We import the pandas along with the matplotlib library
import pandas as pd
import matplotlib.pyplot as plt # (Optional) if you run it in Jupyter

# We create our dataframe
df = pd.DataFrame(index=range(0,10), data={"y1" : range(0,10)})

# We plot the dataframe
df.plot(kind="bar")

# We need to show the plot.
plt.show() # (Optional) if you run it in Jupyter
The simplest way to plot it using Pandas and Matplotlib

You can pass quite a lot of paremeters to the .plot() method.

Such as

  1. A title as title="My amazing title"
  2. xlabel as xlabel="x"
  3. ylabel as ylabel="y"
  4. the type of chart here wen have kind="bar" default is line
  5. etc...

Like so :

# We import the library
import pandas as pd
import matplotlib.pyplot as plt

# We create our dataframe
df = pd.DataFrame(index=range(0,10), data={"y1" : range(0,10)})

# We plot the dataframe
df.plot(title="My amazing title",
        xlabel="X",
        ylabel="y",
        kind="bar")
plt.show()
Using all the parameters shown above

Here you go ! You know how to make a basic bar plot in Python

7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

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

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.