Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e9e20b

Browse files
committed
Use plt.subplots() in usage guide
1 parent ed5b8f4 commit 5e9e20b

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

tutorials/introductory/usage.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
# Matplotlib graphs your data on `~.figure.Figure`\s (i.e., windows, Jupyter
2020
# widgets, etc.), each of which can contain one or more `~.axes.Axes` (i.e., an
2121
# area where points can be specified in terms of x-y coordinates (or theta-r
22-
# in a polar plot, or x-y-z in a 3D plot, etc.). In the following example, we
23-
# create a figure using the `.pyplot.figure` function, an axes on that figure
24-
# using the `.Figure.add_subplot` method, and graph some data on that axes
25-
# using the `.Axes.plot` method:
22+
# in a polar plot, or x-y-z in a 3D plot, etc.). The most simple way of
23+
# creating a figure with an axes is using `.pyplot.subplots`. We can then use
24+
# `.Axes.plot` to draw some data on the axes:
2625

27-
fig = plt.figure() # Create a figure.
28-
ax = fig.add_subplot() # Add an axes to the figure.
26+
fig, ax = plt.subplots() # Create a figure containing a single axes.
2927
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes.
3028

3129
###############################################################################
@@ -64,16 +62,18 @@
6462
# 'special' artists (titles, figure legends, etc), and the **canvas**.
6563
# (Don't worry too much about the canvas, it is crucial as it is the
6664
# object that actually does the drawing to get you your plot, but as the
67-
# user it is more-or-less invisible to you). A figure can have any
68-
# number of :class:`~matplotlib.axes.Axes`, but to be useful should have
65+
# user it is more-or-less invisible to you). A figure can contain any
66+
# number of :class:`~matplotlib.axes.Axes`, but will typically have
6967
# at least one.
7068
#
7169
# The easiest way to create a new figure is with pyplot:
7270

73-
fig = plt.figure() # an empty figure with no axes
74-
fig.suptitle('No axes on this figure') # Add a title so we know which it is
71+
fig = plt.figure() # an empty figure with no Axes
72+
fig, ax = plt.subplots() # a figure with a single Axes
73+
fig, axs = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
7574

76-
fig, ax_lst = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
75+
# It's convenient to create the axes together with the figure, but you can
76+
# also add axes later on, allowing for more complex axes layouts.
7777

7878
###############################################################################
7979
# :class:`~matplotlib.axes.Axes`
@@ -157,8 +157,7 @@
157157
x = np.linspace(0, 2, 100)
158158

159159
# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
160-
fig = plt.figure() # Create a figure.
161-
ax = fig.add_subplot() # Add an axes to the figure.
160+
fig, ax = plt.subplots() # Create a figure and an axes.
162161
ax.plot(x, x, label='linear') # Plot some data on the axes.
163162
ax.plot(x, x**2, label='quadratic') # Plot more data on the axes...
164163
ax.plot(x, x**3, label='cubic') # ... and some more.

0 commit comments

Comments
 (0)