How to Create a Bar Chart in Python using Matplotlib

You may use the following syntax in order to create a bar chart in Python using Matplotlib:

import matplotlib.pyplot as plt

plt.bar(xAxis,yAxis)
plt.title('title name')
plt.xlabel('xAxis name')
plt.ylabel('yAxis name')
plt.show()

Next, you’ll see how to apply the above syntax in practice.

Steps to Create a Bar Chart in Python using Matplotlib

Step 1: Install the Matplotlib package

If you haven’t already done so, install the Matplotlib package in Python using the command below (under Windows):

pip install matplotlib

You can refer to the following guide for the instructions to install a package in Python.

Step 2: Gather the data for the bar chart

Next, gather the data for your bar chart.

For illustration purposes, I prepared the following dataset:

Country GDP_Per_Capita
USA45000
Canada42000
Germany52000
UK49000
France47000

The ultimate goal is to depict the above data using a bar chart.

Step 3: Capture the data in Python

For this step, capture the above dataset in Python. You can capture this dataset using lists:

Country = ['USA','Canada','Germany','UK','France']
GDP_Per_Capita = [45000,42000,52000,49000,47000]

Step 4: Create the bar chart in Python using Matplotlib

Finally, you may use the template below to assist you in depicting the bar chart:

import matplotlib.pyplot as plt

plt.bar(xAxis,yAxis)
plt.title('title name')
plt.xlabel('xAxis name')
plt.ylabel('yAxis name')
plt.show()

For our example, the complete Python code would look as follows:

import matplotlib.pyplot as plt
   
Country = ['USA','Canada','Germany','UK','France']
GDP_Per_Capita = [45000,42000,52000,49000,47000]

plt.bar(Country, GDP_Per_Capita)
plt.title('Country Vs GDP Per Capita')
plt.xlabel('Country')
plt.ylabel('GDP Per Capita')
plt.show()

Run the code, and you’ll get the bar chart below:

Example of chart

You can further style the bar chart using this code:

import matplotlib.pyplot as plt
   
Country = ['USA','Canada','Germany','UK','France']
GDP_Per_Capita = [45000,42000,52000,49000,47000]

New_Colors = ['green','blue','purple','brown','teal']
plt.bar(Country, GDP_Per_Capita, color=New_Colors)
plt.title('Country Vs GDP Per Capita', fontsize=14)
plt.xlabel('Country', fontsize=14)
plt.ylabel('GDP Per Capita', fontsize=14)
plt.grid(True)
plt.show()

You’ll now get the following styled bar chart, where each country is represented by a different color:

How to Create a Bar Chart in Python using Matplotlib

Create a Bar Chart in Python with Pandas DataFrame

So far, you have seen how to create your bar chart using lists.

Alternatively, you can capture the dataset in Python using Pandas DataFrame, and then plot your chart.

Here is the complete code that you may use:

import matplotlib.pyplot as plt
import pandas as pd
   
Data = {'Country': ['USA','Canada','Germany','UK','France'],
        'GDP_Per_Capita': [45000,42000,52000,49000,47000]
       }
df = pd.DataFrame(Data,columns=['Country','GDP_Per_Capita'])

New_Colors = ['green','blue','purple','brown','teal']
plt.bar(df['Country'], df['GDP_Per_Capita'], color=New_Colors)
plt.title('Country Vs GDP Per Capita', fontsize=14)
plt.xlabel('Country', fontsize=14)
plt.ylabel('GDP Per Capita', fontsize=14)
plt.grid(True)
plt.show()

Run the code, and you’ll get the exact same bar chart as you saw in the previous section:

Bar Chart in Python using Matplotlib

You may also want to check the guides below for the steps to: