Skip to content

Commit 15c50ef

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #29272: DOC: Add section on translating between Axes and pyplot interface
1 parent b0e372a commit 15c50ef

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

galleries/users_explain/figure/api_interfaces.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,51 @@ In the explicit interface, this would be:
121121
axs[0].plot([1, 2, 3], [0, 0.5, 0.2])
122122
axs[1].plot([3, 2, 1], [0, 0.5, 0.2])
123123

124+
Translating between the Axes interface and the pyplot interface
125+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126+
You may find either interface in existing code, and unfortunately sometimes even
127+
mixtures. This section describes the patterns for specific operations in both
128+
interfaces and how to translate from one to the other.
129+
130+
- Creating figures is the same for both interfaces: Use the respective `.pyplot`
131+
functions ``plt.figure()``, ``plt.subplots()``, ``plt.subplot_mosaic()``.
132+
For the Axes interface, you typically store the created Figure (and possibly
133+
Axes) in variables for later use. When using the pyplot interface, these
134+
values are typically not stored. Example:
135+
136+
- Axes: ``fig, ax = plt.subplots()``
137+
- pyplot: ``plt.subplots()``
138+
139+
- "Plotting" functions, i.e. functions that add data, are named the same and
140+
have identical parameters on the Axes and in pyplot. Example:
141+
142+
- Axes: ``ax.plot(x, y)``
143+
- pyplot: ``plt.plot(x, y)``
144+
145+
- Functions that retrieve properties are named like the property in pyplot
146+
and are prefixed with ``get_`` on the Axes. Example:
147+
148+
- Axes: ``label = ax.get_xlabel()``
149+
- pyplot: ``label = plt.xlabel()``
150+
151+
- Functions that set properties like the property in pyplot and are prefixed with
152+
``set_`` on the Axes. Example:
153+
154+
- Axes: ``ax.set_xlabel("time")``
155+
- pyplot: ``plt.xlabel("time")``
156+
157+
Here is a short summary of the examples again as a side-by-side comparison:
158+
159+
================== ============================ ========================
160+
Operation Axes interface pyplot interface
161+
================== ============================ ========================
162+
Creating figures ``fig, ax = plt.subplots()`` ``plt.subplots()``
163+
Plotting data ``ax.plot(x, y)`` ``plt.plot(x, y)``
164+
Getting properties ``label = ax.get_xlabel()`` ``label = plt.xlabel()``
165+
Setting properties ``ax.set_xlabel("time")`` ``plt.xlabel("time")``
166+
================== ============================ ========================
167+
168+
124169
Why be explicit?
125170
^^^^^^^^^^^^^^^^
126171

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy