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 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, let’s use the following data about two variables:
- year
- unemployment_rate
Here is the complete data:
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 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 your Line chart using lists.
Alternatively, you may 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) 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.