How to Plot a Line Chart in Python using Matplotlib

In this short guide, you’ll see how to plot a Line chart in Python using Matplotlib.

To start, here is a template that you may use to plot your Line chart:

import matplotlib.pyplot as plt

plt.plot(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 template using a practical example.

Steps to Plot a Line 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 this command (under Windows):

pip install matplotlib

You may check the following guide for the instructions to install a package in Python using PIP.

Step 2: Gather the data for the Line chart

Next, gather the data for your Line chart.

For example, I gathered the following data about two variables for a given economy:

  • Year
  • Unemployment_Rate

Here is how the data looks like:

YearUnemployment_Rate
19209.8
193012
19408
19507.2
19606.9
19707
19806.5
19906.2
20005.5
20106.3

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

Step 3: Capture the data in Python

You can capture the above data in Python using the following two Lists:

Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
Unemployment_Rate = [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]

Step 4: Plot a Line chart in Python using Matplotlib

For the final step, you may use the template below in order to plot the Line chart in Python:

import matplotlib.pyplot as plt

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

Here is how the code would look like for our example:

import matplotlib.pyplot as plt
   
Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
Unemployment_Rate = [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
  
plt.plot(Year, Unemployment_Rate)
plt.title('Unemployment Rate Vs Year')
plt.xlabel('Year')
plt.ylabel('Unemployment Rate')
plt.show()

Run the code in Python and you’ll get the following Line chart:

How to Plot a Line Chart in Python using Matplotlib

You could further style the Line chart using this code:

import matplotlib.pyplot as plt
   
Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
Unemployment_Rate = [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
  
plt.plot(Year, Unemployment_Rate, color='red', marker='o')
plt.title('Unemployment Rate Vs Year', fontsize=14)
plt.xlabel('Year', fontsize=14)
plt.ylabel('Unemployment Rate', fontsize=14)
plt.grid(True)
plt.show()

You’ll then get this styled Line chart:

Plot a Line Chart in Python using Matplotlib

How to Create a Line Chart in Python with Pandas DataFrame

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

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

In that case, the complete code would look as follows:

import pandas as pd
import matplotlib.pyplot as plt
   
Data = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }
  
df = pd.DataFrame(Data,columns=['Year','Unemployment_Rate'])
  
plt.plot(df['Year'], df['Unemployment_Rate'], color='red', marker='o')
plt.title('Unemployment Rate Vs Year', fontsize=14)
plt.xlabel('Year', fontsize=14)
plt.ylabel('Unemployment Rate', fontsize=14)
plt.grid(True)
plt.show()

You’ll then get the exact same Line chart with Pandas DataFrame:

Line Chart in Python