Simple Plotting in Python with matplotlib

In this series, we work on some simpler tasks:

  1. Making a line plot using matplotlib
  2. Downloading a time-series of data from a THREDDS server
  3. Plotting the data using matplotlib

If while reading this blog post you have any questions about what certain words are defined as see this computer programming dictionary forum, which you can view here.

Plotting with matplotlib

matplotlib is a 2D plotting library that is relatively easy to use to produce publication-quality plots in Python. It provides an interface that is easy to get started with as a beginner, but it also allows you to customize almost every part of a plot. matplotlib's gallery provides a good overview of the wide array of graphics matplotlib is capable of creating. We'll just scratch the surface of matplotlib's capabilities here by looking at making some line plots.

This first line tells the Jupyter Notebook interface to set up plots to be displayed inline (as opposed to opening plots in a separate window). This is only needed for the notebook.

%matplotlib inline

The first step is to import the NumPy library, which we will import as np to give us less to type. This library provides an array object we can use to perform mathematics operations, as well as easy ways to make such arrays. We use the linspace function to create an array of 10 values in x, spanning between 0 and 5. We then set y equal to x * x.

import numpy as np

x = np.linspace(0, 5, 10)
y = x * x

Now we want to make a quick plot of these x and y values; for this we'll use matplotlib. First, we import the matplotlib.pyplot module, which provides a simple plotting interface; we import this as plt, again to save typing.

matplotlib has two main top-level plotting objects: Figure and Axes. A Figure represents a single figure for plotting (a single image or figure window), which contains one or more Axes objects. An Axes groups together an x and y axis, and contains all of the various plotting methods that one would want to use.

Below, we use the subplots() function, with no parameters, to quickly create a Figure, fig, and an Axes, ax, for us to plot on. We then use ax.plot(x, y) to create a line plot on the Axes we created; this command uses pairs of values from the x and y arrays to create points defining the line.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(x, y)


Matplotlib provides a wide array of ways to control the appearance of the plot. Below we adjust the line so that it is a thicker, red dashed line. By specifying the marker argument, we tell matplotlib to add a marker at each point; in this case, that marker is a square (s). For more information on linestyles, markers, etc., type help(ax.plot) in a cell, or see the matplotlib plot docs.

fig, ax = plt.subplots()
ax.plot(x, y, color='red', linestyle='--', linewidth=2, marker='s')


Controlling Other Plot Aspects

In addition to controlling the look of the line, matplotlib provides many other features for cutomizing the look of the plot. In our plot, below we:

  • Add gridlines
  • Set labels for the x and y axes
  • Add a title to the plot
fig, ax = plt.subplots()
ax.plot(x, y, color='red')
ax.set_title('f(x) = x * x')


matplotlib also has support for LaTeX-like typesetting of mathematical expressions, called mathtext. This is enabled by surrounding math expressions by $ symbols. Below, we replace the x * x in the title, with the more expressive $x^2$.

fig, ax = plt.subplots()
ax.plot(x, y, color='red')
ax.set_title('$f(x) = x^2$')


Multiple Plots

Often, what we really want is to make multiple plots. This can be accomplished in two ways:

  • Plot multiple lines on a single Axes
  • Combine multiple Axes in a single Figure

First, let's look at plotting multiple lines. This is really simple--just call plot on the Axes you want multiple times:

fig, ax = plt.subplots()
ax.plot(x, x, color='green')
ax.plot(x, x * x, color='red')
ax.plot(x, x**3, color='blue')


Of course, in this plot it isn't clear what each line represents. We can add a legend to clarify the picture; to make it easy for matplotlib to create the legend for us, we can label each plot as we make it:

fig, ax = plt.subplots()
ax.plot(x, x, color='green', label='$x$')
ax.plot(x, x * x, color='red', label='$x^2$')
ax.plot(x, x**3, color='blue', label='$x^3$')
ax.legend(loc='upper left')


Another option for looking at multiple plots is to use multiple Axes; this is accomplished by passing our desired layout to subplots(). The simplest way is to just give it the number of rows and columns; in this case the axes are returned as a two dimensional array of Axes instances with shape (rows, columns).

# Sharex tells subplots that all the plots should share the same x-limit, ticks, etc.
# It also eliminates the redundant labelling
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True)

axes[0, 0].plot(x, x)
axes[0, 0].set_title('Linear')

axes[0, 1].plot(x, x * x)
axes[0, 1].set_title('Squared')

axes[1, 0].plot(x, x ** 3)
axes[1, 0].set_title('Cubic')

axes[1, 1].plot(x, x ** 4)
axes[1, 1].set_title('Quartic')


Of course, that's a little verbose for my liking, not to mention tedious to update if we want to add more labels. So we can also use a loop to plot:

fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True)

titles = ['Linear', 'Squared', 'Cubic', 'Quartic']
y_vals = [x, x * x, x**3, x**4]

# axes.flat returns the set of axes as a flat (1D) array instead
# of the two-dimensional version we used earlier
for ax, title, y in zip(axes.flat, titles, y_vals):
    ax.plot(x, y)


This makes it easy to tweak all of the plots with a consistent style without repeating ourselves. It's also then easier to add, or remove plots and reshape. If you're not familiar with the zip() function below, it's Python's way of iterating (looping) over multiple lists of things together; so each time through the loop the first, second, etc. items from each of the lists is returned. It's one of the built-in parts of Python that makes it so easy to use.


Next time, we'll look at how to acquire more interesting data from a THREDDS server using Siphon. This will culminate in creating a meteogram using MetPy, Siphon, and matplotlib all together. For more information on what was covered today, we suggest looking at:

  • matplotlib's website has many resources for learning more about matplotlib
  • matplotlib's documentation gives more information on using matplotlib
  • matplotlib's gallery is a great place to see visually what matplotlib can do

For more of Unidata's work in Python, see: - Unidata Blog Notebooks (View Here) - Notebooks from Unidata's Annual Python Training Workshop

Was this too much detail? Too slow? Just right? Do you have suggestions on other topics or examples we should cover? Do you have a notebook you would like us to show off? We'd love to have your feedback. You can send a message to the (python-users AT mailing list or send a message to support-python AT You can also leave a comment below, directly on the blog post.


Thank you for this really good post on plotting using Python's matplotlib.

Posted by Erik on July 03, 2016 at 12:40 PM MDT #

You're welcome, I'm glad you found it useful!

Posted by Ryan May on July 05, 2016 at 09:22 AM MDT #

Thank you for this really good post on plotting using Python's matplotlib

Posted by jual-hp-android-murah/ on August 31, 2016 at 02:09 AM MDT #

Post a Comment:
  • HTML Syntax: Allowed
Unidata Developer's Blog
A weblog about software development by Unidata developers*
Unidata Developer's Blog
A weblog about software development by Unidata developers*



News@Unidata blog

Take a poll!

What if we had an ongoing user poll in here?

Browse By Topic
Browse by Topic
« March 2017