Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7405482

Browse filesBrowse files
authored
Merge pull request #29272 from timhoffm/doc-interface-translate
DOC: Add section on translating between Axes and pyplot interface
2 parents cc82d4f + b254c92 commit 7405482
Copy full SHA for 7405482

File tree

Expand file treeCollapse file tree

1 file changed

+45
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+45
-0
lines changed

‎galleries/users_explain/figure/api_interfaces.rst

Copy file name to clipboardExpand all lines: galleries/users_explain/figure/api_interfaces.rst
+45Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.