Understanding Usage of plt, figure, subplot, axes, axis in matplotlib

When working with python libraries, especially for visualization, I usually get confused my number of options available for plotting. Example:

1. plt.plot()

2. ax = plt.subplot()
   ax.plot(x, y)

3. fig1, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

4. f, axarr = plt.subplots(2,2)
   axarr[0,0].imshow(image_datas[0])
   ....

After reading through a bunch of stackoverflow explainations, I compiled them here:


Question 1: What is the difference between drawing plots using plot, axes or figure in matplotlib?


Plot just one figure with (x,y) coordinates

plt.plot(x, y)

If you just want to get one graphic, you can use this way.

Example:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(10)
y = np.random.rand(10)

figure1 = plt.plot(x,y)
Fig 1. Photo via Stackoverflow

Plot one or several figure(s) in the same window

Plot just one figure

ax = plt.subplot()
ax.plot(x, y)

or you can plot multiple figures like this:

fig1, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

This plot 4 figures which are named ax1, ax2, ax3 and ax4 each one but on the same window. This window will be just divided in 4 parts with my example.

Example:

import numpy as np
import matplotlib.pyplot as plt

x1 = np.random.rand(10)
x2 = np.random.rand(10)
x3 = np.random.rand(10)
x4 = np.random.rand(10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)
y3 = np.random.rand(10)
y4 = np.random.rand(10)

figure2, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
ax1.plot(x1,y1)
ax2.plot(x2,y2)
ax3.plot(x3,y3)
ax4.plot(x4,y4)

plt.show()
Fig 2. Photo via Stackoverflow

Another method for multiple plots


import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
new_plot = fig.add_subplot(111)
new_plot.plot(x, y)


Question 2: Difference between “axes” and “axis” in matplotlib?

In the context of matplotlib, axes is not the plural form of axis, it actually denotes the plotting area, including all axis.

Fig 3. Photo via matplotlib old documentation
  • Axes

    This is what you think of as a plot, it is the region of the image with the data space (marked as the inner blue box). A given figure can contain many Axes, but a given Axes object can only be in one Figure. The Axes contains two (or three in the case of 3D) Axis objects (be aware of the difference between Axes and Axis) which take care of the data limits (the data limits can also be controlled via set via the set_xlim() and set_ylim() Axes methods). Each Axes has a title (set via set_title()), an x-label (set via set_xlabel()), and a y-label set via set_ylabel()).

  • Axis

    These are the number-line-like objects (circled in green). They take care of setting the graph limits and generating the ticks (the marks on the axis) and ticklabels (strings labeling the ticks). The location of the ticks is determined by a Locator object and the ticklabel strings are formatted by a Formatter. The combination of the correct Locator and Formatter gives very fine control over the tick locations and labels.



Question 3: What is the difference between plt.subplots() and plt.figure()

In matplotlib, we can plots in two ways like below:

plt.figure(1,figsize=(400,8))

or

fig,ax = plt.subplots()
fig.set_size_inches(400,8)

and though both are correct, they have their differences.

plt.figure just creates a figure (but with no axes in it) whereas plt.subplots takes optional arguments (ex: plt.subplots(2, 2)) to create an array of axes in the figure. Most of the kwargs that plt.figure takes plt.subplots also takes.

plt.figure() is usually used when you want more customization to you axes, such as positions, sizes, colors and etc. You can see artist tutorial for more details. (I personally prefer this for individual plot).

plt.subplots() is recommended for generating multiple subplots in grids. You can also achieve higher flexibility using ‘gridspec’ and ‘subplots’, see details here.

Srishti Yadav
Srishti Yadav
ML Researcher

My research interest include applying computationally intensive machine learning algorithm to image or text based data

Related