|
18 | 18 | # The library is accessible through a variety of operating systems and
|
19 | 19 | # programming environments. The fundamental ideas behind Matplotlib for
|
20 | 20 | # visualizations involve taking data and transforming it through functions and
|
21 |
| -# methods. |
22 |
| -# |
23 |
| -# There are two main ways of producing graphs with Matplotlib, stateful and |
24 |
| -# stateless. Stateful code, using `pyplot`, or stateless code, using Object |
25 |
| -# Oriented Programming (OOP), are the foundation for creating and manipulating |
26 |
| -# data into visualizations. |
| 21 | +# methods. This process occurs on the backend and is not user-facing. For more |
| 22 | +# information regarding manipulating backend capabilities, see """ref""". |
27 | 23 | #
|
28 |
| -# The stateful `pyplot` approach to generate plots is simple. It is helpful |
29 |
| -# for basic plots and for interactive environments, such as Jupyter Notebooks. |
30 |
| -# Users familiar with MATLAB or would like to have Matplotlib automatically |
31 |
| -# create and manage parts of the visualization benefit from using `pyplot` |
32 |
| -# functions to plot their data. |
| 24 | +# There are two main ways of producing graphs with Matplotlib, explicit and |
| 25 | +# implicit. Explicit code, using Object Oriented Programmiong (OOP), and |
| 26 | +# implicit code, using `pyplot`, are the foundation for creating and |
| 27 | +# manipulating data into visualizations. |
33 | 28 | #
|
34 |
| -# Stateless programming, OOP, helps users generalize code and is useful for |
| 29 | +# Explicit programming, OOP, helps users generalize code and is useful for |
35 | 30 | # repeated uses or larger projects. This is also a more robust way of
|
36 | 31 | # controlling customizations for visualizations. Users looking to have control
|
37 | 32 | # over every part of the graph can call methods on each item.
|
38 | 33 | #
|
| 34 | +# The implicit `pyplot` approach to generate plots is simple. It is helpful |
| 35 | +# for basic plots and for interactive environments, such as Jupyter Notebooks. |
| 36 | +# Users familiar with MATLAB or would like to have Matplotlib automatically |
| 37 | +# create and manage parts of the visualization benefit from using `pyplot` |
| 38 | +# functions to plot their data. |
39 | 39 | #
|
40 | 40 | #
|
41 | 41 |
|
|
71 | 71 | #
|
72 | 72 | # `python -m pip install matplotlib`
|
73 | 73 | #
|
| 74 | +# .. note:: |
| 75 | +# |
| 76 | +# If you would like to contribute to Matplotlib, see the developer |
| 77 | +# installation guide for details about the process. |
| 78 | +# |
74 | 79 | # Interactive environments
|
75 | 80 | # ------------------------
|
76 | 81 | #
|
|
84 | 89 | # ========
|
85 | 90 | #
|
86 | 91 | # The common conventions for preparing to plot data involve importing the
|
87 |
| -# necessary libraries with abbreviations for convenience. Both stateful and |
88 |
| -# stateless programming require the following. |
| 92 | +# necessary libraries with abbreviations for convenience. Both implicit and |
| 93 | +# explicit programming require the following. |
89 | 94 |
|
90 | 95 | import matplotlib.pyplot as plt
|
91 | 96 |
|
|
97 | 102 | #
|
98 | 103 | # These are the two common strategies for creating plots with Matplotlib.
|
99 | 104 | #
|
100 |
| -# * Stateful: The programming is designed to remember preceding events or |
101 |
| -# interactions. Matplotlib automatically manages figures and axes. |
102 |
| -# * `pyplot`, most similar to MATLAB and convenient for interactive |
103 |
| -# environments. |
104 |
| -# * Stateless: Code has explicit references to objects. Users manage objects |
| 105 | +# * Explicit: Code has explicit references to objects. Users manage objects |
105 | 106 | # for specific figures and axes and call on methods for manipulating data.
|
106 | 107 | # * Object-oriented programming (OOP), robust control and useful for
|
107 | 108 | # generalized code.
|
108 | 109 | #
|
| 110 | +# * Implicit: The programming is designed to remember preceding events or |
| 111 | +# interactions. Matplotlib automatically manages figures and axes. |
| 112 | +# * `pyplot`, most similar to MATLAB and convenient for interactive |
| 113 | +# environments. |
| 114 | +# |
109 | 115 | # .. note::
|
110 | 116 | #
|
111 |
| -# The Matplotlib community does not recommend interchanging stateful and |
112 |
| -# stateless strategies. When using one as standard, all code should follow |
113 |
| -# the same strategy. Switching back and forth between stateful and |
114 |
| -# stateless programming can yield errors. |
| 117 | +# The Matplotlib community does not recommend interchanging explicit and |
| 118 | +# implicit strategies. When using one as standard, all code should follow |
| 119 | +# the same strategy. Switching back and forth between explicit and |
| 120 | +# implicit programming can yield errors. |
115 | 121 | #
|
116 | 122 | # For other techniques of creating plots with Matplotlib, refer to
|
117 |
| -# """insert reference""". |
| 123 | +# """ref""". |
118 | 124 | #
|
119 | 125 | # Data
|
120 | 126 | # ----
|
|
147 | 153 | # Other containers, such as `pandas` data objects, may not work as
|
148 | 154 | # intended.
|
149 | 155 | #
|
150 |
| -# Stateful: `pyplot` |
151 |
| -# ------------------ |
152 |
| -# |
153 |
| -# To use stateful programming for Matplotlib involves using the `pyplot` |
154 |
| -# module. The figure and axes are automatically generated by the module. |
155 |
| -# Pass data through as arguments using methods within the module. |
156 |
| -# Additional parts of the figure are also available through the module |
157 |
| -# methods. |
158 |
| - |
159 |
| -# Stateful programming with pyplot |
160 |
| - |
161 |
| -x = months |
162 |
| -y1 = income |
163 |
| -y2 = chk_acct_09 |
164 |
| -y3 = svg_acct_09 |
165 |
| - |
166 |
| -plt.plot(x, y1, label='Income') |
167 |
| -plt.plot(x, y2, label='Checking Account') |
168 |
| -plt.plot(x, y3, label='Savings Account') |
169 |
| -plt.xlabel('Month') # Module methods generate parts of figure. |
170 |
| -plt.ylabel('USD') |
171 |
| -plt.title("Personal Financial Tracking from 2009") |
172 |
| -plt.legend() |
173 |
| - |
174 |
| -############################################################################## |
175 |
| -# |
176 |
| -# In the example above, the `pyplot` module contains its own methods of |
177 |
| -# actionable tasks for the data. The `plt.plot` plots data as a line graph |
178 |
| -# with various keyword arguments as customizable options. The module also |
179 |
| -# includes other methods for generating parts of the visualization. These parts |
180 |
| -# use different methods from the OOP approach. |
181 |
| -# |
182 |
| -# Stateless: Object Oriented Programming (OOP) |
| 156 | +# Explicit: Object Oriented Programming (OOP) |
183 | 157 | # --------------------------------------------
|
184 | 158 | #
|
185 |
| -# To use stateless programming for Matplotlib involves using a single instance |
| 159 | +# To use explicit programming for Matplotlib involves using a single instance |
186 | 160 | # of the `pyplot` module to unpack a set or sets of explicit figure and axes.
|
187 | 161 | # Each axes has its own methods to graph data. In addition, each axes also
|
188 | 162 | # uses separate methods to create and manage parts of a figure. These methods
|
189 |
| -# are different from those of the stateful programming approach. |
| 163 | +# are different from those of the implicit programming approach. |
190 | 164 |
|
191 |
| -# Stateless programming with OOP |
| 165 | +# Explicit programming with OOP |
192 | 166 |
|
193 |
| -y4 = chk_acct_10 |
194 |
| -y5 = svg_acct_10 |
| 167 | +x = months |
| 168 | +y1 = income |
| 169 | +y2 = chk_acct_09 |
| 170 | +y3 = svg_acct_09 |
195 | 171 |
|
196 | 172 | fig, ax = plt.subplots() # Figure & axes unpacked separately with module.
|
197 | 173 |
|
198 | 174 | ax.plot(x, y1, label='Checking Account')
|
199 |
| -ax.plot(x, y4, label='Savings Account') |
200 |
| -ax.plot(x, y5, label='Income') |
| 175 | +ax.plot(x, y2, label='Savings Account') |
| 176 | +ax.plot(x, y3, label='Income') |
201 | 177 | ax.set_xlabel('Month') # Axes use separate methods to manage parts of figure.
|
202 | 178 | ax.set_ylabel('USD')
|
203 | 179 | ax.set_title('Personal Financial Tracking from 2010')
|
|
216 | 192 | # instances of managing objects, the specfic axes refers to OOP usage and
|
217 | 193 | # manages the respective data.
|
218 | 194 | #
|
| 195 | +# Implicit: `pyplot` |
| 196 | +# ------------------ |
| 197 | +# |
| 198 | +# To use implicit programming for Matplotlib involves using the `pyplot` |
| 199 | +# module. The figure and axes are automatically generated by the module. |
| 200 | +# Pass data through as arguments using methods within the module. |
| 201 | +# Additional parts of the figure are also available through the module |
| 202 | +# methods. |
| 203 | + |
| 204 | +# Implicit programming with pyplot |
| 205 | + |
| 206 | +y4 = chk_acct_10 |
| 207 | +y5 = svg_acct_10 |
| 208 | + |
| 209 | +plt.plot(x, y1, label='Income') |
| 210 | +plt.plot(x, y4, label='Checking Account') |
| 211 | +plt.plot(x, y5, label='Savings Account') |
| 212 | +plt.xlabel('Month') # Module methods generate parts of figure. |
| 213 | +plt.ylabel('USD') |
| 214 | +plt.title("Personal Financial Tracking from 2009") |
| 215 | +plt.legend() |
| 216 | + |
| 217 | +############################################################################## |
| 218 | +# |
| 219 | +# In the example above, the `pyplot` module contains its own methods of |
| 220 | +# actionable tasks for the data. The `plt.plot` plots data as a line graph |
| 221 | +# with various keyword arguments as customizable options. The module also |
| 222 | +# includes other methods for generating parts of the visualization. These parts |
| 223 | +# use different methods from the OOP approach. |
| 224 | +# |
219 | 225 | # .. note::
|
220 | 226 | #
|
221 | 227 | # The names and spelling for methods may be similar for both `pyplot` and
|
|
243 | 249 | # * Axes are subplots within the figure. They contain figure elements and
|
244 | 250 | # are responsible for plotting and configuring additional details.
|
245 | 251 | # * Note: Axes and Axis are not synonymous. Axis refers to
|
246 |
| -# """insert reference""". |
| 252 | +# """ref""". |
247 | 253 | #
|
248 | 254 | # Multiple Graphs within a Figure
|
249 | 255 | # -------------------------------
|
250 | 256 | #
|
251 |
| -# For multiple graphs using a single figure, stateful and stateless approaches |
| 257 | +# For multiple graphs using a single figure, explicit and implicit approaches |
252 | 258 | # use a similar convention for mapping out multiple axes. Matplotlib manages
|
253 | 259 | # more than one axes in a two-dimensional matrix. They are arranged by row
|
254 | 260 | # amount and then by column amount. The third argument represents the specific
|
|
257 | 263 | # When looking for more complex solutions to multiple graphs within a figure,
|
258 | 264 | # use the GridSpec module to organize the layout.
|
259 | 265 |
|
260 |
| -# Stateful with pyplot |
261 |
| - |
262 |
| -plt.subplot(1, 2, 1) |
263 |
| -plt.plot(x, y1, label='Income') |
264 |
| -plt.plot(x, y2, label='Checking') |
265 |
| -plt.plot(x, y1, color='green', label='Savings') |
266 |
| -plt.xticks(x, months, rotation=270) |
267 |
| -plt.title('2009', fontsize='small') |
268 |
| - |
269 |
| -plt.subplot(1, 2, 2) |
270 |
| -plt.plot(x, y1, label='Income') |
271 |
| -plt.plot(x, y4, label='Checking') |
272 |
| -plt.plot(x, y5, color='green', label='Savings') |
273 |
| -plt.xticks(x, months, rotation=270) |
274 |
| -plt.title('2009', fontsize='small') |
275 |
| -plt.legend(bbox_to_anchor=(1, 1), loc='upper left') |
276 |
| - |
277 |
| -plt.suptitle('Personal Financial Tracking', weight='black') |
278 |
| -plt.tight_layout() |
| 266 | +# Explicit with OOP |
279 | 267 |
|
280 |
| -############################################################################## |
281 |
| -# |
282 |
| -# The `pyplot` example above uses two axes to graph data. In each instance, |
283 |
| -# Matplotlib auotmatically manages the specific axes so that each action of |
284 |
| -# plotting data does not interfere with the previous instance. |
285 |
| - |
286 |
| -# Stateless with OOP |
287 |
| - |
288 |
| -fig, (ax1, ax2) = plt.subplots(2, 1, sharey='row', |
| 268 | +fig, (ax1, ax2) = plt.subplots(1, 2, sharey='row', |
289 | 269 | figsize=[8, 4], constrained_layout=True)
|
290 |
| -# Note the different method name for OOP. |
291 | 270 |
|
292 |
| -fig.suptitle('Personal Financial Tarcking \'09 - \'10', |
| 271 | +fig.suptitle('Personal Financial Tracking \'09 - \'10', |
293 | 272 | fontsize=16, weight='black')
|
294 | 273 |
|
295 | 274 | ax1.plot(x, y1, label='Income')
|
|
310 | 289 | # The OOP example above also uses two axes to graph the data. However, the OOP
|
311 | 290 | # approach must refer to an explicitly generated axes after creating both the
|
312 | 291 | # figure and axes.
|
| 292 | + |
| 293 | +# Implicit with pyplot |
| 294 | + |
| 295 | +plt.subplot(1, 2, 1) # Note the different method name for implicit. |
| 296 | +plt.plot(x, y1, label='Income') |
| 297 | +plt.plot(x, y2, label='Checking') |
| 298 | +plt.plot(x, y1, color='green', label='Savings') |
| 299 | +plt.xticks(x, months, rotation=270) |
| 300 | +plt.title('2009', fontsize='small') |
| 301 | + |
| 302 | +plt.subplot(1, 2, 2) |
| 303 | +plt.plot(x, y1, label='Income') |
| 304 | +plt.plot(x, y4, label='Checking') |
| 305 | +plt.plot(x, y5, color='green', label='Savings') |
| 306 | +plt.xticks(x, months, rotation=270) |
| 307 | +plt.title('2009', fontsize='small') |
| 308 | +plt.legend(bbox_to_anchor=(1, 1), loc='upper left') |
| 309 | + |
| 310 | +plt.suptitle('Personal Financial Tracking', weight='black') |
| 311 | +plt.tight_layout() |
| 312 | + |
| 313 | +############################################################################## |
| 314 | +# |
| 315 | +# The `pyplot` example above uses two axes to graph data. In each instance, |
| 316 | +# Matplotlib auotmatically manages the specific axes so that each action of |
| 317 | +# plotting data does not interfere with the previous instance. |
0 commit comments