Python:Plotting/Subplots

From PrattWiki
Revision as of 02:03, 20 September 2020 by DukeEgr93 (talk | contribs)
Jump to navigation Jump to search

There are some examples in the main Python:Plotting page showing how to set up subplots within a figure window. Here are some more examples, where each example assumes

import numpy as np
import matplotlib.pyplot as plt

has already run. Note: once you have your subplots created, labeled, and titled, you definitely want to run

fig.tight_layout()

before saving; Python does not do a great job with leaving space for things with subplots.

Basics

There are three different ways to create subplots:

  • fig.add_subplot() needs you to create the figure handle first (probably with fig=plt.figure() and then you can use that figure variable to create one set of axes within an array of axes.
    • The three main arguments for fig.add_subplot(nrows, ncols, index) will establish how many rows and columns you want to break the figure into and then which one of those subplots you want to work with. This method can be especially useful if you want to build a figure with several non-overlapping subplots of different shapes - more on that below.
  • plt.subplots() allows you to create the figure and the axes handles at the same time.
    • The two main arguments for plt.subplots(nrows, ncols) will establish how many rows and columns you want to break the figure into. It will return a figure handle as well an array of axes handles (1D array if creating a single column or row, 2D array if there is more than one row and more than one column). This is useful if you have an array of axes and you are planning to use all of them. You can also give the typical figure keyword arguments such as num=1, clear=True
  • fig.subplots() needs you to create the figure handle first (probably with fig=plt.figure() and then you can use that figure variable to create an entire array of axes.
    • The two main arguments for fig.subplots(nrows, ncols) will establish how many rows and columns you want to break the figure into. It will return an array of axes handles (1D array if creating a single column or row, 2D array if there is more than one row and more than one column). This is useful if you have an array of axes and you are planning to use all of them. The only real difference between this version and the figure based one above is that this one will accept keyword arguments to be passed to the axes rather than to the figure. This will become important when creating 3D plots.

Single Subplot For The Whole Figure

fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)

or

fig, ax = plt.subplots(num=1, clear=True)

or

fig = plt.figure(num=1, clear=True)

Single Row or Column of Subplots

If you want to have a single row with four columns of subplots, you can either set up four variables, one for each, or you can set up a single variable that stores all four axes handles. In the latter case, the variable storing the axes will be a 1D array of handles, so you will access each one with a single index value.

fig = plt.figure(num=1, clear=True)
ax1 = fig.add_subplot(1, 4, 1)
ax2 = fig.add_subplot(1, 4, 2)
ax3 = fig.add_subplot(1, 4, 3)
ax4 = fig.add_subplot(1, 4, 4)

or

fig, ax = plt.subplots(1, 4, num=1, clear=True)

If you want to plot 10 uniformly distributed random numbers in the second subplot, for the first case you would use:

ax2.plot(np.random.uniform(size=10))

and for the second case you would use

ax[1].plot(np.random.uniform(size=10))