Anatomy of a Matplotlib Figure

Anatomy of a Matplotlib Figure#

Before we start customizing plots or generating more complex plots, it’s useful to know the components of a matplotlib figure.

%matplotlib inline

import matplotlib, matplotlib.pyplot as plt # import statements
fig = plt.figure() # create empty figure with no axes
plt.show() # show output
fig, ax = plt.subplots() # create figure with single axis
plt.show() # show output
fig, axs = plt.subplots(2, 2) # create a figure with a 2x2 axes grid
plt.show() # show output

Figure#

Figure: A figure object that can include multiple Axes or plots; a Figure contains at least one Axes. Having multiple Axes in the same Figure is useful when creating side-by-side visualizations or a dashboard-style collection of visualizations.

Axes#

In matplotlib syntax, Axes are what we would think of as a single plot, where data is plotted. A Figure can contain many Axes, but a given Axes object can only be in one Figure. For cartesian coordinate plane visualizations, an Axes contains two Axis objects.

Axis#

matplotlib works in a Cartesian coordinate system, with an X (horizontal) and Y (vertical) axis. In a matplotlib plot, the Axis objects set graph limits and generate tick marks and labels.

  • The location of ticks is determined by a Locator object.

  • Tick labels are strings formatted using Formatter.

Everything Else (Artists)#

The other components of the Figure include things like axis labels, marker or line style, tick labels, figure title, etc. These are all referred to as Artists in matplotlib documentation. Knowing how to configure or customize these plot components is not just about aesthetics–in many cases, customizing a plot is necessary for readability.