How to bar plot in Python

1 min

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