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

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

plt.plot(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 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:

pip install matplotlib

Step 2: Gather the data for the Line chart

Next, gather the data for your Line chart.

For example, let’s use the following data about two variables:

  • year
  • unemployment_rate

Here is the complete dataset:

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

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

plt.plot(x_axis, y_axis)
plt.title("title name")
plt.xlabel("x_axis name")
plt.ylabel("y_axis name")
plt.show()

Here is the code 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 Line chart.

You can 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()

How to Create a Line Chart in Python with Pandas DataFrame

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

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

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)

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 same Line chart with Pandas DataFrame.

Additional charts: