|
| 1 | +""" |
| 2 | +*************** |
| 3 | +Getting Started |
| 4 | +*************** |
| 5 | +
|
| 6 | +This tutorial covers some basic usage patterns and best-practices to |
| 7 | +help you get started with Matplotlib. |
| 8 | +""" |
| 9 | + |
| 10 | +############################################################################## |
| 11 | +# |
| 12 | +# Introduction |
| 13 | +# ============ |
| 14 | +# |
| 15 | +# Matplotlib is a Python library providing tools for users to create |
| 16 | +# visualizations with data. |
| 17 | +# |
| 18 | +# The library is accessible through a variety of operating systems and |
| 19 | +# programming environments. The fundamental ideas behind Matplotlib for |
| 20 | +# visualizations involve taking data and transforming it through functions and |
| 21 | +# methods. This process occurs on the backend and is not user-facing. For more |
| 22 | +# information regarding manipulating backend capabilities, see """ref""". |
| 23 | +# |
| 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. |
| 28 | +# |
| 29 | +# Explicit programming, OOP, helps users generalize code and is useful for |
| 30 | +# repeated uses or larger projects. This is also a more robust way of |
| 31 | +# controlling customizations for visualizations. Users looking to have control |
| 32 | +# over every part of the graph can call methods on each item. |
| 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 | +# |
| 40 | +# |
| 41 | + |
| 42 | + |
| 43 | +############################################################################## |
| 44 | +# |
| 45 | +# Requirements |
| 46 | +# ============ |
| 47 | +# |
| 48 | +# Matplotlib is a Python library and an installed version of Python 3.6 or |
| 49 | +# higher is required. Depending on your operating system, Python may already |
| 50 | +# be installed on your machine. |
| 51 | +# |
| 52 | +# Installing Maptlotlib is required in order to generate plots with the |
| 53 | +# library. You can install Matplotlib for your own development environment(s) |
| 54 | +# or use a third-party package distribution. |
| 55 | +# |
| 56 | +# Third-party package distributions, such as |
| 57 | +# `Anaconda <https://www.anaconda.com/>`_, |
| 58 | +# `ActiveState <https://www.activestate.com/activepython/downloads>`_, |
| 59 | +# or `WinPython <https://winpython.github.io/>`_, |
| 60 | +# already provide Matplotlib and its dependencies. They have the added benefit |
| 61 | +# of including other scientific Python libraries as well. These packages work |
| 62 | +# as is and do not require additional installations. |
| 63 | +# |
| 64 | +# Installation from source |
| 65 | +# ------------------------ |
| 66 | +# |
| 67 | +# In order to install Matplotlib from the source directory, you can run the |
| 68 | +# following command line executions using Python and installer program `pip` |
| 69 | +# for the latest version of Matplotlib and its dependencies. This will compile |
| 70 | +# the library from the current directory on your machine. |
| 71 | +# |
| 72 | +# `python -m pip install matplotlib` |
| 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 | +# |
| 79 | +# Interactive environments |
| 80 | +# ------------------------ |
| 81 | +# |
| 82 | +# The Matplotlib community suggests using `IPython <https://ipython.org/>`_ |
| 83 | +# through `Jupyter <https://jupyter.org/index.html>`_ as the primary |
| 84 | +# interactive environment. |
| 85 | + |
| 86 | +############################################################################## |
| 87 | +# |
| 88 | +# Plotting |
| 89 | +# ======== |
| 90 | +# |
| 91 | +# The common conventions for preparing to plot data involve importing the |
| 92 | +# necessary libraries with abbreviations for convenience. Both implicit and |
| 93 | +# explicit programming require the following. |
| 94 | + |
| 95 | +import matplotlib.pyplot as plt |
| 96 | + |
| 97 | +############################################################################## |
| 98 | +# |
| 99 | +# The `pyplot` module in Matplotlib is a collection of functions. The module's |
| 100 | +# functions create, manage, and manipulate the current figure and the plotting |
| 101 | +# area. |
| 102 | +# |
| 103 | +# These are the two common strategies for creating plots with Matplotlib. |
| 104 | +# |
| 105 | +# * Explicit: Code has explicit references to objects. Users manage objects |
| 106 | +# for specific figures and axes and call on methods for manipulating data. |
| 107 | +# * Object-oriented programming (OOP), robust control and useful for |
| 108 | +# generalized code. |
| 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 | +# |
| 115 | +# .. note:: |
| 116 | +# |
| 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. |
| 121 | +# |
| 122 | +# For other techniques of creating plots with Matplotlib, refer to |
| 123 | +# """ref""". |
| 124 | +# |
| 125 | +# Data |
| 126 | +# ---- |
| 127 | +# |
| 128 | +# The Matplotlib library manages data in the form of iterables and/or |
| 129 | +# sequenced items. These can also take the form of NumPy arrays like |
| 130 | +# `numpy.array` or `numpy.ma.masked_array`. All plotting functions take these |
| 131 | +# data structures. |
| 132 | +# |
| 133 | + |
| 134 | +# Sample Data |
| 135 | + |
| 136 | +months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
| 137 | + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
| 138 | +income = [950, 950, 950, 950, 950, 950, |
| 139 | + 950, 950, 950, 950, 950, 950] |
| 140 | +chk_acct_09 = [1250, 1325, 1200, 1220, 1100, 1055, |
| 141 | + 1255, 1090, 1190, 1205, 1205, 1180] |
| 142 | +svg_acct_09 = [1000, 1050, 1100, 1150, 1200, 1250, |
| 143 | + 1300, 1350, 1400, 1450, 1500, 1550] |
| 144 | +chk_acct_10 = [1180, 1270, 1280, 1280, 1260, 1140, |
| 145 | + 1270, 1160, 1120, 1250, 1270, 1160] |
| 146 | +svg_acct_10 = [1550, 1600, 1650, 1700, 1750, 1800, |
| 147 | + 1850, 1900, 1950, 2000, 2050, 2100] |
| 148 | + |
| 149 | +############################################################################## |
| 150 | +# |
| 151 | +# .. note:: |
| 152 | +# |
| 153 | +# Other containers, such as `pandas` data objects, may not work as |
| 154 | +# intended. |
| 155 | +# |
| 156 | +# Explicit: Object Oriented Programming (OOP) |
| 157 | +# -------------------------------------------- |
| 158 | +# |
| 159 | +# To use explicit programming for Matplotlib involves using a single instance |
| 160 | +# of the `pyplot` module to unpack a set or sets of explicit figure and axes. |
| 161 | +# Each axes has its own methods to graph data. In addition, each axes also |
| 162 | +# uses separate methods to create and manage parts of a figure. These methods |
| 163 | +# are different from those of the implicit programming approach. |
| 164 | + |
| 165 | +# Explicit programming with OOP |
| 166 | + |
| 167 | +x = months |
| 168 | +y1 = income |
| 169 | +y2 = chk_acct_09 |
| 170 | +y3 = svg_acct_09 |
| 171 | + |
| 172 | +fig, ax = plt.subplots() # Figure & axes unpacked separately with module. |
| 173 | + |
| 174 | +ax.plot(x, y1, label='Checking Account') |
| 175 | +ax.plot(x, y2, label='Savings Account') |
| 176 | +ax.plot(x, y3, label='Income') |
| 177 | +ax.set_xlabel('Month') # Axes use separate methods to manage parts of figure. |
| 178 | +ax.set_ylabel('USD') |
| 179 | +ax.set_title('Personal Financial Tracking from 2010') |
| 180 | +ax.legend() |
| 181 | + |
| 182 | +############################################################################## |
| 183 | +# |
| 184 | +# For the OOP example, the figure and axes are unpacked from the module using |
| 185 | +# a single instance of `pyplot`. This convention uses `plt.subplots()` and |
| 186 | +# defaults to one figure, `fig`, and one axes, `ax`. The section below on |
| 187 | +# customizations contains additional information about multiple visulizations |
| 188 | +# and other modifications. |
| 189 | +# |
| 190 | +# Using the OOP approach allows for `fig` and `ax` to use separate methods to |
| 191 | +# manipulate the visualization. Instead of using the module `pyplot` for all |
| 192 | +# instances of managing objects, the specfic axes refers to OOP usage and |
| 193 | +# manages the respective data. |
| 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 | +# |
| 225 | +# .. note:: |
| 226 | +# |
| 227 | +# The names and spelling for methods may be similar for both `pyplot` and |
| 228 | +# OOP approaches. Errors may occur when using the wrong corresponding |
| 229 | +# method. Confirm with the documentation API for specific method names |
| 230 | +# according to your programming. |
| 231 | + |
| 232 | +############################################################################## |
| 233 | +# |
| 234 | +# Customizations |
| 235 | +# ============== |
| 236 | +# |
| 237 | +# There are two main parts to building a visualization with Matplotlib, the |
| 238 | +# figure and the axes. |
| 239 | +# |
| 240 | +# Components of Matplotlib Figure |
| 241 | +# ------------------------------- |
| 242 | +# |
| 243 | +# The image below depicts each visible element of a Matplotlib graph. |
| 244 | +# |
| 245 | +# * Figure |
| 246 | +# * The figure is the working space for the programming. All visible |
| 247 | +# objects on a graph are located within the figure. |
| 248 | +# * Axes |
| 249 | +# * Axes are subplots within the figure. They contain figure elements and |
| 250 | +# are responsible for plotting and configuring additional details. |
| 251 | +# * Note: Axes and Axis are not synonymous. Axis refers to |
| 252 | +# """ref""". |
| 253 | +# |
| 254 | +# Multiple Graphs within a Figure |
| 255 | +# ------------------------------- |
| 256 | +# |
| 257 | +# For multiple graphs using a single figure, explicit and implicit approaches |
| 258 | +# use a similar convention for mapping out multiple axes. Matplotlib manages |
| 259 | +# more than one axes in a two-dimensional matrix. They are arranged by row |
| 260 | +# amount and then by column amount. The third argument represents the specific |
| 261 | +# axes involved. |
| 262 | +# |
| 263 | +# When looking for more complex solutions to multiple graphs within a figure, |
| 264 | +# use the GridSpec module to organize the layout. |
| 265 | + |
| 266 | +# Explicit with OOP |
| 267 | + |
| 268 | +fig, (ax1, ax2) = plt.subplots(1, 2, sharey='row', |
| 269 | + figsize=[8, 4], constrained_layout=True) |
| 270 | + |
| 271 | +fig.suptitle('Personal Financial Tracking \'09 - \'10', |
| 272 | + fontsize=16, weight='black') |
| 273 | + |
| 274 | +ax1.plot(x, y1, label='Income') |
| 275 | +ax1.plot(x, y2, label='Checking') |
| 276 | +ax1.plot(x, y3, color='green', label='Savings') |
| 277 | +ax1.set_xticklabels(months, rotation=270) |
| 278 | +ax1.set_title('2009', fontsize='small') |
| 279 | +ax1.legend(loc='upper left') |
| 280 | + |
| 281 | +ax2.plot(x, y1, label='Income') |
| 282 | +ax2.plot(x, y2, label='Checking') |
| 283 | +ax2.plot(x, y3, color='green', label='Savings') |
| 284 | +ax2.set_xticklabels(months, rotation=270) |
| 285 | +ax2.set_title('2010', fontsize='small') |
| 286 | + |
| 287 | +############################################################################## |
| 288 | +# |
| 289 | +# The OOP example above also uses two axes to graph the data. However, the OOP |
| 290 | +# approach must refer to an explicitly generated axes after creating both the |
| 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