32
32
# | Explicit, Object Oriented | Implicit, ``pyplot`` |
33
33
# | Programming (OOP) | |
34
34
# +====================================+====================================+
35
- # | Users explicitly create and manage | Automatically manages Figure and |
36
- # | all Figure elements. | Axes. |
35
+ # | Users explicitly create and manage | The Matplotlib library implicitly |
36
+ # | all Figure elements. | manages Figure and Axes. |
37
37
# +------------------------------------+------------------------------------+
38
38
# | Useful for repeated code use, | Helpful for quickly graphing data |
39
39
# | generalization, robust | when using interactive |
55
55
#
56
56
# Implicit programming with ``pyplot`` is simpler. It is helpful for basic
57
57
# plots and for interactive environments, such as Jupyter Notebooks. Users
58
- # familiar with MATLAB or wish to have Matplotlib create and manage parts of
58
+ # familiar with MATLAB or wishing to have Matplotlib create and manage parts of
59
59
# the visualization in state-based programming benefit from using ``pyplot``.
60
60
# Using implicit programming acts as a convenient shortcut for generating
61
61
# visualizations. New users to Matplotlib may experience difficulty
75
75
# +====================================+====================================+
76
76
# | :: | :: |
77
77
# | | |
78
- # | fig, ax = plt.subplots() | plt.plot([1, 2, 3],[1, 2, 3]) |
79
- # | ax.plot([1, 2, 3],[1, 2, 3]) | |
78
+ # | fig, ax = plt.subplots() | plt.plot([1, 2, 3], [1, 2, 3]) |
79
+ # | ax.plot([1, 2, 3], [1, 2, 3]) | |
80
80
# | | |
81
81
# +------------------------------------+------------------------------------+
82
82
# | `.pyplot.subplots` generates a | :mod:`matplotlib.pyplot` creates |
83
83
# | `~.figure.Figure` and one or | implicit Figure and Axes if |
84
84
# | more `~.axes.Axes` explicitly. | there are no pre-existing |
85
85
# | `.Axes.plot` plots the data. | elements and `.pyplot.plot` plots |
86
- # | | the data. |
86
+ # | | the data. This also plots over any |
87
+ # | | existing Figure if applicable. |
87
88
# +------------------------------------+------------------------------------+
88
89
# | .. plot:: | .. plot:: |
89
90
# | | |
90
- # | fig, ax = plt.subplots() | plt.plot([1,2, 3],[1,2, 3]) |
91
- # | ax.plot([1, 2, 3],[1, 2, 3]) | |
91
+ # | fig, ax = plt.subplots() | plt.plot([1, 2, 3], [1, 2, 3]) |
92
+ # | ax.plot([1, 2, 3], [1, 2, 3]) | |
92
93
# | | |
93
94
# +------------------------------------+------------------------------------+
94
95
#
103
104
# higher* is required. Depending on your operating system, Python may already
104
105
# be installed on your machine.
105
106
#
106
- # Installing Maptlotlib is required in order to generate graphs with the
107
+ # Installing Matplotlib is required in order to generate graphs with the
107
108
# library. Install Matplotlib for your own development environment manually or
108
109
# use a third-party package distribution.
109
110
#
140
141
#
141
142
# - The ``pyplot`` module in Matplotlib is a collection of functions. The
142
143
# module's functions create, manage, and manipulate the current Figure and
143
- # the plotting area. The abbreviation as ``plt`` is the standard shortcut.
144
+ # the plotting area. The ``plt`` abbreviation is the standard shortcut.
144
145
#
145
146
146
147
import numpy as np
149
150
150
151
##############################################################################
151
152
#
152
- # - NumPy is a common scientific Python library that benefits users with
153
- # additional tools for manipulating data.
153
+ # - ` NumPy <https://numpy.org/doc/stable/index.html#>`_ is a common scientific
154
+ # Python library that benefits users working with numerical data.
154
155
# - The ``functools`` module helps manage functions that act on or return
155
156
# other functions. The `Pie Chart Examples`_ section note contains more
156
157
# information about the purpose of this module.
157
158
#
159
+ #
160
+ #
158
161
# Two Approaches for Creating Graphs
159
162
# ----------------------------------
160
163
#
227
230
# Explicit: Object Oriented Programming (OOP)
228
231
# --------------------------------------------
229
232
#
230
- # Explicit programming for Matplotlib involves calling the method ``subplots``
231
- # in the ``pyplot`` module once. This unpacks a group of an explicit Figure and
232
- # Axes. More than one Axes is configurable; however, each Axes only corresponds
233
- # to a single Figure.
233
+ # Explicit programming for Matplotlib involves calling the function
234
+ # `pyploy.subplots` in the ``pyplot`` module once. This returns a group of an
235
+ # explicit Figure and Axes to be unpacked as part of variable assignment. More
236
+ # than one Axes is configurable; however, each Axes only corresponds to a
237
+ # single Figure.
234
238
#
235
239
# Each Axes has its own methods to graph data. In addition, each Axes
236
- # also uses separate methods to create and manage parts of a Figure. These
237
- # methods are different from those of the implicit programming approach.
240
+ # also uses separate methods to create and manage objects within a Figure.
241
+ # These methods are different from those of the implicit programming approach.
238
242
239
243
# Explicit programming with OOP
240
244
241
- # Assigning sample data to labeled variables.
245
+ # Assigning sample data to variables.
242
246
x = months
243
247
y1 = income
244
248
y2 = chk_acct_09
266
270
267
271
##############################################################################
268
272
#
269
- # The module ``pyplot`` for the OOP example unpacks the Figure and Axes.
270
- # This convention uses ``plt.subplots()`` and defaults to one Figure, ``fig``,
271
- # and one Axes, ``ax``. The variable names are common shorthand terms. Any
272
- # naming conventions also work.
273
+ # The module ``pyplot`` for the explicit example uses a function that returns
274
+ # the Figure and Axes. This convention uses ``plt.subplots()``. It defaults
275
+ # to one Figure, ``fig``, and one Axes, ``ax``. The variable names are common
276
+ # shorthand terms and any naming conventions also work.
273
277
#
274
278
# The `Configuration`_ section below contains additional information about
275
279
# manipulating visuals, multiple visualizations, and other modifications.
276
280
#
277
281
# Using explicit programming allows for ``fig`` and ``ax`` to use separate
278
- # methods to manipulate the visualization. Specific Figures and Axes manage
279
- # data components with their own respective methods.
282
+ # methods to manage objects within the visualization. Specific Figures and
283
+ # Axes manage data components with their own respective methods.
280
284
#
281
285
#
282
286
# Implicit: ``pyplot``
283
287
# --------------------
284
288
#
285
289
# Implicit programming for Matplotlib centers around using the ``pyplot``
286
- # module. The module automatically generates Figure and Axes. Methods and
290
+ # module. The module implicitly generates the Figure and Axes. Methods and
287
291
# functions within the module take incoming data as arguments. Additional parts
288
292
# of the Figure are also available through the module methods.
289
293
307
311
308
312
##############################################################################
309
313
#
310
- # In the example above, the ``pyplot`` module contains its own methods of
314
+ # In the example above, the ``pyplot`` module contains its own functions of
311
315
# actionable tasks for the data. The ``plt.plot`` plots data as a line graph
312
316
# with various keyword arguments as customizable options. The module also
313
317
# includes other methods for generating parts of the visualization. These parts
364
368
#
365
369
# :class:`~matplotlib.axes.Axes`
366
370
#
367
- # Axes are subplots within the Figure. They contain Figure elements and
371
+ # Axes are subplots within the Figure. They contain Matplotlib objects and
368
372
# are responsible for plotting and configuring additional details. Each
369
373
# Figure can contain multiple Axes, but each Axes is specific to one
370
374
# Figure.
371
375
#
372
376
# In a Figure, each Axes contains any number of visual elements. Axes are
373
377
# configurable for more than one type of visualization of data. From the
374
378
# `Plotting`_ section above, the Axes in both explicit and implicit strategies
375
- # contain multiple types of visualizations of data on a single Axes. Each of
376
- # these types are specific to the Axes they are in.
379
+ # contain multiple types of visualizations of data on a single Axes.
380
+
381
+ # Each of these types are specific to the Axes they are in. In the example, the
382
+ # two plots each have one Axes. These Axes each have multiple plot lines. The
383
+ # lines as objects are not shared between the two plots even though the data is
384
+ # shared.
377
385
#
378
386
# Matplotlib Axes also integrate with other Python libraries. In Axes-based
379
387
# interfaces, other libraries take an Axes object as input. Libraries such as
472
480
# | | :doc:`/tutorials/text/annotations` |
473
481
# +------------------------------+--------------------------------------------+
474
482
#
475
- # For complete information about available methods for Artists, refer to the
476
- # table below.
483
+ # For complete information about available methods for creating new Artists,
484
+ # refer to the table below.
477
485
#
478
486
# +------------------------------------+------------------------------------+
479
487
# | Explicit | Implicit |
@@ -594,7 +602,7 @@ def autopct_format(percent, group):
594
602
#
595
603
# The pie chart below adds configurations with keyword arguments for
596
604
# ``explode``, ``autopct``, ``startangle``, and ``shadow``. These keyword
597
- # arguments help to manipulate the Artists.
605
+ # arguments help to define the display of Artists.
598
606
599
607
# Explicit
600
608
@@ -603,7 +611,7 @@ def autopct_format(percent, group):
603
611
# The explode keyword argument uses explode variable data to separate
604
612
# respective wedges from center.
605
613
# The autopct keyword argument takes formatting strings and functions to
606
- # generate text within wedge. '%1.1f%%' is the string formatter.
614
+ # generate text within each wedge. '%1.1f%%' is the string formatter.
607
615
# The startangle keyword argument changes where first wedge spans. Angles start
608
616
# at 0 degrees on X-axis and move counterclockwise.
609
617
# The shadow keyword argument toggles a shadow on the visual.
@@ -676,8 +684,8 @@ def autopct_format(percent, group):
676
684
ax .set_title ('Average Monthly Income Expenses' )
677
685
ax .axis ('equal' )
678
686
679
- # The Figure method tight_layout() manages space between all Artists to
680
- # maximize visiblity on Figure. This method also contains various
687
+ # The Figure method tight_layout() adjusts spacing between all Artists to
688
+ # maximize visiblity on the Figure. This method also contains various
681
689
# parameters for configuration.
682
690
fig .tight_layout ()
683
691
@@ -759,18 +767,18 @@ def autopct_format(percent, group):
759
767
# the explicit approach refers to an explicitly generated Axes after creating
760
768
# both the Figure and Axes.
761
769
#
762
- # In the unpacking process, numerous Axes are assigned to a single variable.
770
+ # In the unpacking process, multiple Axes are assigned to a single variable.
763
771
# To reference a specific Axes, indexing the location of the respective Axes
764
772
# as a matrix through the single variable works as well.
765
773
#
766
774
# The code below demonstrates indexing multiple Axes::
767
775
#
768
- # fig, ax = plt.subplots(2,2)
776
+ # fig, ax = plt.subplots(2, 2)
769
777
#
770
- # ax[0,0].bar([1,2, 3],[1,2, 3])
771
- # ax[0,1].plot([3,2, 1],[3,2, 1])
778
+ # ax[0,0].bar([1, 2, 3], [1, 2, 3])
779
+ # ax[0,1].plot([3, 2, 1], [3, 2, 1])
772
780
# ax[1,0].hist(hist_data)
773
- # ax[1,1].imshow([[1,2], [2,1]])
781
+ # ax[1,1].imshow([[1, 2], [2, 1]])
774
782
#
775
783
#
776
784
# The method `matplotlib.figure.Figure.subplot_mosaic` also generates Axes in
@@ -783,10 +791,10 @@ def autopct_format(percent, group):
783
791
# ax_dict = fig.subplot_mosaic([['bar', 'plot'],
784
792
# ['hist', 'image']])
785
793
#
786
- # ax_dict['bar'].bar([1,2, 3],[1,2, 3])
787
- # ax_dict['plot'].plot([3,2, 1],[3,2, 1])
794
+ # ax_dict['bar'].bar([1, 2, 3], [1, 2, 3])
795
+ # ax_dict['plot'].plot([3, 2, 1], [3, 2, 1])
788
796
# ax_dict['hist'].hist(hist_data)
789
- # ax_dict['image'].imshow([[1,2], [2,1]])
797
+ # ax_dict['image'].imshow([[1, 2], [2, 1]])
790
798
#
791
799
# Implicit
792
800
# ^^^^^^^^
@@ -816,7 +824,7 @@ def my_plotter(ax, data1, data2, param_dict):
816
824
X data
817
825
data2 : array
818
826
Y data
819
- param_dict : dict()
827
+ param_dict : dict
820
828
Dictionary of keyword arguments passes to method
821
829
822
830
Returns
0 commit comments