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.
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:
Year | Unemployment_Rate |
1920 | 9.8 |
1930 | 12 |
1940 | 8 |
1950 | 7.2 |
1960 | 6.9 |
1970 | 7 |
1980 | 6.5 |
1990 | 6.2 |
2000 | 5.5 |
2010 | 6.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:
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:
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:
This will draw a line that passes through the points [-1, 1] and [12, 4], and another one that passes through the points [1, 3] et [10, 2]
x1 are the x coordinates of the points for the first line, y1 are the y coordinates for the same -- the elements in x1 and y1 must be in sequence.
x2 and y2 are the same for the other line.
import matplotlib.pyplot as plt
x1, y1 = [-1, 12], [1, 4]
x2, y2 = [1, 10], [3, 2]
plt.plot[x1, y1, x2, y2, marker = 'o']
plt.show[]
I suggest you spend some time reading / studying the basic tutorials found on the very rich matplotlib website to familiarize yourself with the library.
What if I don't want line segments?
[edit]:
As shown by @thomaskeefe, starting with matplotlib 3.3, this is now builtin as a convenience:
plt.axline[[x1, y1], [x2, y2]]
, rendering the following obsolete.
There are no direct ways to have lines extend to infinity... matplotlib will either resize/rescale the plot so that the furthest point will be on the boundary and the other inside, drawing line segments in effect; or you must choose points outside of the boundary of the surface you want to set visible, and set limits for the x and y axis.
As follows:
import matplotlib.pyplot as plt
x1, y1 = [-1, 12], [1, 10]
x2, y2 = [-1, 10], [3, -1]
plt.xlim[0, 8], plt.ylim[-2, 8]
plt.plot[x1, y1, x2, y2, marker = 'o']
plt.show[]