matplotlib Creating an axes


Example

There are two main ways to create an axes in matplotlib: using pyplot, or using the object-oriented API.

Using pyplot:

import matplotlib.pyplot as plt

ax = plt.subplot(3, 2, 1)  # 3 rows, 2 columns, the first subplot

Using the object-oriented API:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(3, 2, 1)

The convenience function plt.subplots() can be used to produce a figure and collection of subplots in one command:

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(ncols=2, nrows=1)  # 1 row, 2 columns