How to Track Changes Over Time with a Line Chart, Python, and Matplotlib
2021-01-17 19:45:11 |
- Linux Ubuntu 20.04
- Windows 10
- macOS Catalina
Matplotlib is a Python Library used to create interactive static and dynamic plots. Additionally, you can create 3-dimensional plots to visualize data in space. Because Matplotlib is mostly concerned with data visualization, we have to provide data using various libraries like Numpy (The Numerical Python). Once the data is provided, Matplotlib can work its magic and produce graphs and charts. In this tutorial, we're going to use the latest version of Matplotlib (3.3.3) and Python 3.8.5
Matplotlib was developed by John D. Hunter in 2002 and first released in 2003 under the BSD license. Originally built upon the Numpy and Scipy framework, it enabled interactive Matlab-like plotting from the iPython command line. This early functionality was a collaborative effort between Fernando Perez and John Hunter, and intended as an iPython patch. Perez, unable to review the patch to work on his PhD, compelled Hunter to release the project on his own.
Some Key Matplotlib Features
- Line graphs, bar graphs, histograms
- Scatter plot, polar plots, and pie charts
- Reading CSV files for plotting
- Loading and saving plot figures
How to Get Started with Matplotlib
It helps to have be familiar with Python fundamentals, like data types, loops, functions, conditionals, modules, packages, virtual environments, etc. If you need a crash course, navigate to the Python Developer section of our tutorials.
How to Set Up a Project Skeleton
How to Create Python Project Files with Windows 10 PowerShell 2.0+
cd ~ New-Item -ItemType "directory" -Path ".\matplotlib-project" cd matplotlib-project virtualenv venv .\venv\Scripts\activate
To verify that the virtual environment is active, make sure (venv) is in the PowerShell command prompt. For example, (venv) PS C:\Users\username\matplotlib-project>
How to Create Python Project Files with Linux Ubuntu 14.04+ or macOS
cd ~ mkdir matplotlib-project cd matplotlib-project virtualenv -p python3 venv source venv/bin/activate
To verify that the virtual environment is active, make sure (venv) is in the terminal command prompt.
This will create the following files and folders, and activate the virtual environment.
▾ matplotlib-project/ ▸ venv/
Installing Matplotlib with Pip
This tutorial requires you to install a specific version of Matplotlib with pip3 install matplotlib==3.3.3. To get the plot to display in a window, you can install PyQt5 with pip3 install PyQt5==5.15.2.
We want to install a target version of these libraries because there may be API changes between the time this article is written to the time you read it.
How to Create a Line Chart with Matplotlib
By utilizing matplotplib.pyplot functions, we can represent data peaks and pits with lines. Line graphs present data points above a baseline (usually zero), indicating the frequency/value of each point. Value/category names are displayed along the x axis. The higher the value, the higher its point is rendered along the y axis. A line usually connects all the points (hence, the name "line graph").
Line graphs are great for tracking changes over time, especially small changes. Use a line chart when you want to depict trends, and bar graphs when you want to compare large differences between categories.
Start by creating a file called line_graph.py and populating it with the code, below.
from matplotlib import pyplot as plt # Data arr_x = [2, 4, 8, 10, 12] arr_y = [1, 2, 3, 4, 5] plt.plot(arr_x, arr_y) # (x, y) plt.savefig("plot.png") plt.show()
Explanation of the Code
Line 1: First we import the pyplot module from matplotlib as the shorthand plt.
Lines 4-5: We create two lists with some random numerical values to be plotted. These lists get passed into the plt.plot(x, y) function on line 7, which prepares the data for plotting.
Line 9: We save the output chart to a png file.
Line 10: We render the line graph to the screen with plt.show().
Customizing Our Line Graph
This examples introduces us to some customization options.
from matplotlib import pyplot as plt import numpy # Title of the plot plt.title("Matplotlib Line Graph") # Apply style/theme plt.style.use("fivethirtyeight") # Randomized data arr_x = numpy.array(numpy.arange(50)) arr_y = numpy.array(numpy.random.randint(0, 500, 50)) plt.plot(arr_x, arr_y, color='g', marker='.', linewidth=1) # Axis titles plt.xlabel("x_axis") plt.ylabel("y_axis") # Render plt.savefig("plot.png") plt.show()
Explanation of the Code
Line 5 and lines 17-18 demonstrate that we can customize plot and axis titles through pyplot functions title(), xlabel() and ylabel.
Line 8 shows us how to apply a different theme.
Line1 11-12 shows us how to utilize numpy and to generate a random data set within a specified range of integers.
Line 14 demonstrates keyword additional keyword arguments we can pass in.
How to Create a Multiple Line Chart Using a Real World Example
A multiple line graph allows us to visualize multiple sets of data points. The example below overlays multiple lines that plot the popularity of different programming languages from the past few years.
As you've observed, we've created three lists containing the number of developers for each language, per year.`
In this tutorial, we introduced you to Matplotlib, and how to set up a Matplotlib project for producing line graphs and multiple line graphs. We also demonstrated how to track changes over time with a real world example.