@@ -48,47 +48,48 @@ completely, leaving a purely object-oriented approach.
48
48
49
49
.. _pylab :
50
50
51
- Matplotlib, pylab, and pyplot : how are they related?
51
+ Matplotlib, pyplot and pylab : how are they related?
52
52
====================================================
53
53
54
- Matplotlib is the whole package; :mod: `pylab ` is a module in matplotlib
55
- that gets installed alongside :mod: ` matplotlib ` ; and :mod: `matplotlib.pyplot `
56
- is a module in matplotlib.
54
+ Matplotlib is the whole package; :mod: `matplotlib.pyplot `
55
+ is a module in matplotlib; and :mod: `pylab ` is a module
56
+ that gets installed alongside :mod: ` matplotlib ` .
57
57
58
- Pyplot provides the state-machine interface to the underlying OO plotting
59
- library in matplotlib. This means that figures and axes are implicitly
60
- and automatically created to achieve the desired plot. For example,
61
- calling ``plot `` from pyplot will automatically create the necessary
62
- figure and axes to achieve the desired plot. Setting a title will
63
- then automatically set that title to the current axes object::
58
+ Pyplot provides the state-machine interface to the underlying
59
+ object-oriented plotting library. The state-machine implicitly and
60
+ automatically creates figures and axes to achieve the desired
61
+ plot. For example::
64
62
65
63
66
- .. sourcecode:: ipython
64
+ import matplotlib.pyplot as plt
65
+ import numpy as np
67
66
68
- In [14]: %matplotlib
69
- Using matplotlib backend: Qt4Agg
67
+ x = np.linspace(0, 2, 100)
70
68
71
- In [15]: import matplotlib.pyplot as plt
69
+ plt.plot(x, x, label='linear')
70
+ plt.plot(x, x**2, label='quadratic')
71
+ plt.plot(x, x**3, label='cubic')
72
72
73
- In [16]: plt.plot(range(10), range(10) )
74
- Out[16]: [<matplotlib.lines.Line2D at 0x7fdfef9be1d0>]
73
+ plt.xlabel('x label' )
74
+ plt.ylabel('y label')
75
75
76
- In [17]: plt.title("Simple Plot")
77
- Out[17]: <matplotlib.text.Text at 0x7fdfee53d0d0>
76
+ plt.title("Simple Plot")
78
77
79
- This is very convenient for interactive use, however
80
- because the commands have side-effects (altering the global state)
81
- using many :mod: `matplotlib.pyplot ` commands in scripts or functions
82
- can lead to unexpected and difficult to track down bugs.
78
+ plt.legend()
83
79
84
- Pylab is a convenience module that imports pyplot (for plotting) and
85
- numpy functionality (for mathematics and for working with arrays) in a
86
- single namespace. You can than bulk import from pylab to get an even
87
- more MATLAB-like environment. This seems convenient for interactive
88
- calculations and plotting, as it (barely) minimizes typing, however it
89
- is not recommended as it clobbers your namespace. As with :mod: `pyplot `,
90
- it is not recommended to use :mod: `pylab ` in scripts and bulk importing
91
- :mod: `pylab ` in scripts is discouraged as with all bulk importing.
80
+ plt.show()
81
+
82
+ The first call to ``plt.plot `` will automatically create the necessary
83
+ figure and axes to achieve the desired plot. Subsequent calls to
84
+ ``plt.plot `` re-use the current axes and each add another line.
85
+ Setting the title, legend, and axis labels also automatically use the
86
+ current axes and set the title, create the legend, and label the axis
87
+ respectively.
88
+
89
+ :mod: `pylab ` is a convenience module that bulk imports
90
+ :mod: `matplotlib.pyplot ` (for plotting) and :mod: `numpy `
91
+ (for mathematics and working with arrays) in a single name space.
92
+ Although many examples use :mod: `pylab `, it is no longer recommended.
92
93
93
94
For non-interactive plotting it is suggested
94
95
to use pyplot to create the figures and then the OO interface for
@@ -112,7 +113,7 @@ The only caveat is to avoid mixing the coding styles for your own code.
112
113
Of the different styles, there are two that are officially supported.
113
114
Therefore, these are the preferred ways to use matplotlib.
114
115
115
- For the preferred pyplot style, the imports at the top of your
116
+ For the pyplot style, the imports at the top of your
116
117
scripts will typically be::
117
118
118
119
import matplotlib.pyplot as plt
0 commit comments