|
| 1 | +.. _usage-faq: |
| 2 | + |
| 3 | +*************** |
| 4 | +Usage |
| 5 | +*************** |
| 6 | + |
| 7 | +.. contents:: |
| 8 | + :backlinks: none |
| 9 | + |
| 10 | +.. _pylab: |
| 11 | + |
| 12 | +Matplotlib, pylab, and pyplot: how are they related? |
| 13 | +==================================================== |
| 14 | + |
| 15 | +Matplotlib is the whole package; :mod:`pylab` is a module in matplotlib |
| 16 | +that gets |
| 17 | +installed alongside :mod:`matplotlib`; and :mod:`matplotlib.pyplot` is a |
| 18 | +module in matplotlib. |
| 19 | + |
| 20 | +Pyplot provides a Matlab-style state-machine interface to |
| 21 | +the underlying object-oriented plotting library in matplotlib. |
| 22 | + |
| 23 | +Pylab combines the pyplot functionality (for plotting) with the numpy |
| 24 | +functionality (for mathematics and for working with arrays) |
| 25 | +in a single namespace, making that namespace |
| 26 | +(or environment) even more Matlab-like. This is what you get if |
| 27 | +you use the |
| 28 | +*ipython* shell with the *-pylab* option, which imports everything |
| 29 | +from pylab and makes plotting fully interactive. |
| 30 | + |
| 31 | +We have been gradually converting the matplotlib examples |
| 32 | +from pure Matlab-style, using "from pylab import \*", to a preferred |
| 33 | +style in which pyplot is used for some convenience functions, either |
| 34 | +pyplot or the object-oriented style is used for the remainder of the |
| 35 | +plotting code, and numpy is used explicitly for numeric array operations. |
| 36 | + |
| 37 | +In this preferred style, the imports at the top are:: |
| 38 | + |
| 39 | + import matplotlib.pyplot as plt |
| 40 | + import numpy as np |
| 41 | + |
| 42 | +Then one calls, for example, np.arange, np.zeros, np.pi, plt.figure, |
| 43 | +plt.plot, plt.show, etc. |
| 44 | + |
| 45 | +Example, pure Matlab-style:: |
| 46 | + |
| 47 | + from pylab import * |
| 48 | + x = arange(0, 10, 0.2) |
| 49 | + y = sin(x) |
| 50 | + plot(x, y) |
| 51 | + show() |
| 52 | + |
| 53 | +Now in preferred style, but still using pyplot interface:: |
| 54 | + |
| 55 | + import matplotlib.pyplot as plt |
| 56 | + import numpy as np |
| 57 | + x = np.arange(0, 10, 0.2) |
| 58 | + y = np.sin(x) |
| 59 | + plt.plot(x, y) |
| 60 | + plt.show() |
| 61 | + |
| 62 | +And using pyplot convenience functions, but object-orientation for the rest:: |
| 63 | + |
| 64 | + import matplotlib.pyplot as plt |
| 65 | + import numpy as np |
| 66 | + x = np.arange(0, 10, 0.2) |
| 67 | + y = np.sin(x) |
| 68 | + fig = plt.figure() |
| 69 | + ax = fig.add_subplot(111) |
| 70 | + ax.plot(x, y) |
| 71 | + plt.show() |
| 72 | + |
| 73 | +So, why do all the extra typing required as one moves away from the pure |
| 74 | +matlab-style? For very simple things like this example, the only |
| 75 | +advantage is educational: the wordier styles are more explicit, more |
| 76 | +clear as to where things come from and what is going on. For more |
| 77 | +complicated applications, the explicitness and clarity become |
| 78 | +increasingly valuable, and the richer and more complete object-oriented |
| 79 | +interface will likely make the program easier to write and maintain. |
| 80 | + |
| 81 | + |
| 82 | + |
0 commit comments