|
101 | 101 | # interactions. Matplotlib automatically manages figures and axes.
|
102 | 102 | # * `pyplot`, most similar to MATLAB and convenient for interactive
|
103 | 103 | # environments.
|
104 |
| -# * Stateless: Code has explicit references to objects. Users create objects |
| 104 | +# * Stateless: Code has explicit references to objects. Users manage objects |
105 | 105 | # for specific figures and axes and call on methods for manipulating data.
|
106 | 106 | # * Object-oriented programming (OOP), robust control and useful for
|
107 | 107 | # generalized code.
|
|
175 | 175 | #
|
176 | 176 | # In the example above, the `pyplot` module contains its own methods of
|
177 | 177 | # actionable tasks for the data. The `plt.plot` plots data as a line graph
|
178 |
| -# with various keyword arguments as additional customizations. The module also |
| 178 | +# with various keyword arguments as customizable options. The module also |
179 | 179 | # includes other methods for generating parts of the visualization. These parts
|
180 | 180 | # use different methods from the OOP approach.
|
181 | 181 | #
|
|
184 | 184 | #
|
185 | 185 | # To use stateless programming for Matplotlib involves using a single instance
|
186 | 186 | # of the `pyplot` module to unpack a set or sets of explicit figure and axes.
|
187 |
| -# Each axes has its own methods to plot data. In addition, each axes also |
| 187 | +# Each axes has its own methods to graph data. In addition, each axes also |
188 | 188 | # uses separate methods to create and manage parts of a figure. These methods
|
189 | 189 | # are different from those of the stateful programming approach.
|
190 | 190 |
|
|
228 | 228 | # Customizations
|
229 | 229 | # ==============
|
230 | 230 | #
|
231 |
| -# There are four main parts to building a visualization with Matplotlib, the |
232 |
| -# figure, the axes, the axis, and the artist(s). |
| 231 | +# There are two main parts to building a visualization with Matplotlib, the |
| 232 | +# figure and the axes. |
233 | 233 | #
|
234 | 234 | # Components of Matplotlib Figure
|
235 | 235 | # -------------------------------
|
236 | 236 | #
|
237 | 237 | # The image below depicts each visible element of a Matplotlib graph.
|
238 | 238 | #
|
239 | 239 | # * Figure
|
| 240 | +# * The figure is the working space for the programming. All visible |
| 241 | +# objects on a graph are located within the figure. |
240 | 242 | # * Axes
|
241 |
| -# * Axis |
242 |
| -# * Artist(s) |
| 243 | +# * Axes are subplots within the figure. They contain figure elements and |
| 244 | +# are responsible for plotting and configuring additional details. |
| 245 | +# * Note: Axes and Axis are not synonymous. Axis refers to |
| 246 | +# """insert reference""". |
| 247 | +# |
| 248 | +# Multiple Graphs within a Figure |
| 249 | +# ------------------------------- |
| 250 | +# |
| 251 | +# For multiple graphs using a single figure, stateful and stateless approaches |
| 252 | +# use a similar convention for mapping out multiple axes. Matplotlib manages |
| 253 | +# more than one axes in a two-dimensional matrix. They are arranged by row |
| 254 | +# amount and then by column amount. The third argument represents the specific |
| 255 | +# axes involved. |
| 256 | +# |
| 257 | +# When looking for more complex solutions to multiple graphs within a figure, |
| 258 | +# use the GridSpec module to organize the layout. |
| 259 | + |
| 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() |
| 279 | + |
| 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', |
| 289 | + figsize=[8, 4], constrained_layout=True) |
| 290 | +# Note the different method name for OOP. |
| 291 | + |
| 292 | +fig.suptitle('Personal Financial Tarcking \'09 - \'10', |
| 293 | + fontsize=16, weight='black') |
| 294 | + |
| 295 | +ax1.plot(x, y1, label='Income') |
| 296 | +ax1.plot(x, y2, label='Checking') |
| 297 | +ax1.plot(x, y3, color='green', label='Savings') |
| 298 | +ax1.set_xticklabels(months, rotation=270) |
| 299 | +ax1.set_title('2009', fontsize='small') |
| 300 | +ax1.legend(loc='upper left') |
| 301 | + |
| 302 | +ax2.plot(x, y1, label='Income') |
| 303 | +ax2.plot(x, y2, label='Checking') |
| 304 | +ax2.plot(x, y3, color='green', label='Savings') |
| 305 | +ax2.set_xticklabels(months, rotation=270) |
| 306 | +ax2.set_title('2010', fontsize='small') |
| 307 | + |
| 308 | +############################################################################## |
| 309 | +# |
| 310 | +# The OOP example above also uses two axes to graph the data. However, the OOP |
| 311 | +# approach must refer to an explicitly generated axes after creating both the |
| 312 | +# figure and axes. |
0 commit comments