@@ -121,6 +121,51 @@ In the explicit interface, this would be:
121
121
axs[0].plot([1, 2, 3], [0, 0.5, 0.2])
122
122
axs[1].plot([3, 2, 1], [0, 0.5, 0.2])
123
123
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
+
124
169
Why be explicit?
125
170
^^^^^^^^^^^^^^^^
126
171
0 commit comments