Difference between revisions of "Python:Plotting/Subplots"

From PrattWiki
Jump to navigation Jump to search
Line 18: Line 18:
 
* [https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=subplots#matplotlib.figure.Figure.subplots fig.subplots()] needs you to create the figure handle first (probably with <code>fig=plt.figure()</code> and then you can use that figure variable to create an entire array of axes.
 
* [https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=subplots#matplotlib.figure.Figure.subplots fig.subplots()] needs you to create the figure handle first (probably with <code>fig=plt.figure()</code> and then you can use that figure variable to create an entire array of axes.
 
** The two main arguments for <code>fig.subplots(nrows, ncols)</code> 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.
 
** The two main arguments for <code>fig.subplots(nrows, ncols)</code> 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 ===
+
== Single Subplot For The Whole Figure ==
 +
=== Using <code>fig.add_subplot()</code> ===
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
fig = plt.figure(num=1, clear=True)
 
fig = plt.figure(num=1, clear=True)
 
ax = fig.add_subplot(1, 1, 1)
 
ax = fig.add_subplot(1, 1, 1)
 
</syntaxhighlight>
 
</syntaxhighlight>
or
+
=== Using <code>plt.subplots()</code> ===
 +
The default for this command is to make a single subplot, so there is no row or column information necesary.
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
fig, ax = plt.subplots(num=1, clear=True)
 
fig, ax = plt.subplots(num=1, clear=True)
 
</syntaxhighlight>
 
</syntaxhighlight>
or
+
=== Using <code>fig.subplots()</code> ===
 +
The default for this command is to make a single subplot, so there is no row or column information necesary.
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
fig = plt.figure(num=1, clear=True)
 
fig = plt.figure(num=1, clear=True)
 +
ax= fig.subplots()
 +
</syntaxhighlight>
  
</syntaxhighlight>
+
== Single Row or Column of Subplots ==
 +
Imagine you want to have a single row with four columns of subplots.  You can either set up four variables, one to access each axes, 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.
  
=== Single Row or Column of Subplots ===
+
=== Using <code>fig.add_subplot()</code> ===
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 handlesIn 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.
+
This version requires that you set each up individuallyThis can be useful if each set of axes has different characteristics (for instance, if some have 3D graphs and others do not).
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
fig = plt.figure(num=1, clear=True)
 
fig = plt.figure(num=1, clear=True)
Line 42: Line 48:
 
ax4 = fig.add_subplot(1, 4, 4)
 
ax4 = fig.add_subplot(1, 4, 4)
 
</syntaxhighlight>
 
</syntaxhighlight>
or
+
 
 +
If you want to plot 10 uniformly distributed random numbers in the second subplot, you would use:
 +
<syntaxhighlight lang="Python">
 +
ax2.plot(np.random.uniform(size=10))
 +
</syntaxhighlight>
 +
=== Using <code>plt.subplots()</code> ===
 +
The first two positional arguments are the number of rows and the number of columns of plots to make.  In this case, the <code>ax</code> variable will be a 1D array with four elements - one per subplot.
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
fig, ax = plt.subplots(1, 4, num=1, clear=True)
 
fig, ax = plt.subplots(1, 4, num=1, clear=True)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
If you want to plot 10 uniformly distributed random numbers in the second subplot, for the first case you would use:
+
If you want to plot 10 uniformly distributed random numbers in the second subplot, you would use:
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
ax2.plot(np.random.uniform(size=10))
+
ax[1].plot(np.random.uniform(size=10))
 +
</syntaxhighlight>
 +
=== Using <code>fig.subplots()</code> ===
 +
The first two positional arguments are the number of rows and the number of columns of plots to make.  In this case, the <code>ax</code> variable will be a 1D array with four elements - one per subplot.
 +
<syntaxhighlight lang="Python">
 +
fig = plt.figure(num=1, clear=True)
 +
ax = fig.subplots(1, 4)
 
</syntaxhighlight>
 
</syntaxhighlight>
and for the second case you would use
+
 
 +
If you want to plot 10 uniformly distributed random numbers in the second subplot, you would use:
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
ax[1].plot(np.random.uniform(size=10))
 
ax[1].plot(np.random.uniform(size=10))
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 02:11, 20 September 2020

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

Using fig.add_subplot()

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

Using plt.subplots()

The default for this command is to make a single subplot, so there is no row or column information necesary.

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

Using fig.subplots()

The default for this command is to make a single subplot, so there is no row or column information necesary.

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

Single Row or Column of Subplots

Imagine you want to have a single row with four columns of subplots. You can either set up four variables, one to access each axes, 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.

Using fig.add_subplot()

This version requires that you set each up individually. This can be useful if each set of axes has different characteristics (for instance, if some have 3D graphs and others do not).

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)

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

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

Using plt.subplots()

The first two positional arguments are the number of rows and the number of columns of plots to make. In this case, the ax variable will be a 1D array with four elements - one per subplot.

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

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

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

Using fig.subplots()

The first two positional arguments are the number of rows and the number of columns of plots to make. In this case, the ax variable will be a 1D array with four elements - one per subplot.

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

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

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