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

x_axis = ['value_1', 'value_2', 'value_3', ...]
y_axis = ['value_1', 'value_2', 'value_3', ...]

plt.bar(x_axis, y_axis)
plt.title('title name')
plt.xlabel('x_axis name')
plt.ylabel('y_axis 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, let’s use the following dataset:

country gdp_per_capita
A 45000
B 42000
C 52000
D 49000
E 47000

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 = ['A', 'B', 'C', 'D', 'E']
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

x_axis = ['value_1', 'value_2', 'value_3', ...]
y_axis = ['value_1', 'value_2', 'value_3', ...]

plt.bar(x_axis, y_axis)
plt.title('title name')
plt.xlabel('x_axis name')
plt.ylabel('y_axis name')
plt.show()

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

import matplotlib.pyplot as plt
   
country = ['A', 'B', 'C', 'D', 'E']
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.

You can further style the bar chart using this code:

import matplotlib.pyplot as plt
   
country = ['A', 'B', 'C', 'D', 'E']
gdp_per_capita = [45000, 42000, 52000, 49000, 47000]

colors = ['green', 'blue', 'purple', 'brown', 'teal']
plt.bar(country, gdp_per_capita, color=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 a styled bar chart, where each country is represented by a different color.

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': ['A', 'B', 'C', 'D', 'E'],
        'gdp_per_capita': [45000, 42000, 52000, 49000, 47000]
       }
df = pd.DataFrame(data)

colors = ['green','blue','purple','brown','teal']
plt.bar(df['country'], df['gdp_per_capita'], color=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 that you saw in the previous section.

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