|
19 | 19 | # Matplotlib graphs your data on `~.figure.Figure`\s (i.e., windows, Jupyter |
20 | 20 | # widgets, etc.), each of which can contain one or more `~.axes.Axes` (i.e., an |
21 | 21 | # 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: |
26 | 25 |
|
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. |
29 | 27 | ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes. |
30 | 28 |
|
31 | 29 | ############################################################################### |
|
64 | 62 | # 'special' artists (titles, figure legends, etc), and the **canvas**. |
65 | 63 | # (Don't worry too much about the canvas, it is crucial as it is the |
66 | 64 | # 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 |
69 | 67 | # at least one. |
70 | 68 | # |
71 | 69 | # The easiest way to create a new figure is with pyplot: |
72 | 70 |
|
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 |
75 | 74 |
|
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. |
77 | 77 |
|
78 | 78 | ############################################################################### |
79 | 79 | # :class:`~matplotlib.axes.Axes` |
|
157 | 157 | x = np.linspace(0, 2, 100) |
158 | 158 |
|
159 | 159 | # 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. |
162 | 161 | ax.plot(x, x, label='linear') # Plot some data on the axes. |
163 | 162 | ax.plot(x, x**2, label='quadratic') # Plot more data on the axes... |
164 | 163 | ax.plot(x, x**3, label='cubic') # ... and some more. |
|
0 commit comments