How to Track Changes Over Time with a Line Chart, Python, and Matplotlib

2021-01-17 19:45:11 | #programming #python #dataviz

Tested On

  • 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

Matplotlib logo

  • 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.

File: line_graph.py

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

A simple line chart depicting a line trending upwards

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.

File: line_graph_custom.py

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

A line graph depicting a line plot of random values between 0 and 500

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.

File: multiple_line_graph.py

import matplotlib.pyplot as plt

plt.style.use("fivethirtyeight")
plt.title("Popular Languages")
plt.xlabel("year")
plt.ylabel("developers(in millions)")

# Number of developers from past 5 years, in millions
python = [4, 5, 6, 7.4, 8.2]
javascript = [7, 8.2, 9.9, 10.2, 11.7]
cpp = [2.5, 3.7, 4.9, 5.2, 5.6]

years = [2015, 2016, 2017, 2018, 2019]

plt.plot(years, python, label="Python")
plt.plot(years, javascript, label="JavaScript")
plt.plot(years, cpp, label="C++")

plt.legend()
plt.xticks(ticks=years, labels=[2015, 2016, 2017, 2018, 2019])

plt.savefig("plot.png")
plt.show()

Multiple line chart depicting the popularity of Python, JavaScript, and C++ between 2015-2019, with JavaScript in the lead, Python in second, and C++ in third

As you've observed, we've created three lists containing the number of developers for each language, per year.

`

Conclusion

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.

Comments

You must log in to comment. Don't have an account? Sign up for free.

Subscribe to comments for this post

Want To Receive More Free Content?

Would you like to receive free resources, tailored to help you reach your IT goals? Get started now, by leaving your email address below. We promise not to spam. You can also sign up for a free account and follow us on and engage with the community. You may opt out at any time.



Hire Us for IT and Consulting Services









Contact Us

Do you have a specific IT problem that needs solving or just have a general IT question? Use the contact form to get in touch with us and an IT professional will be with you, momentarily.

Services

We offer web development, enterprise software development, QA & testing, google analytics, domains and hosting, databases, security, IT consulting, and other IT-related services.

Free IT Tutorials

Head over to our tutorials section to learn all about working with various IT solutions.

We Noticed Adblock Running

Because we offer a variety of free programming tools and resources to our visitors, we rely on ad revenue to keep our servers up. Would you consider disabling Adblock for our site and clicking the "Refresh Page" button?

Contact