Skip to content

Commit 7f79ca5

Browse files
committed
Merge pull request matplotlib#3247 from tacaswell/usage_faq
Usage FAQ update.
2 parents 92ff77b + adc9ec4 commit 7f79ca5

File tree

1 file changed

+91
-51
lines changed

1 file changed

+91
-51
lines changed

doc/faq/usage_faq.rst

Lines changed: 91 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,52 @@ completely, leaving a purely object-oriented approach.
4848

4949
.. _pylab:
5050

51-
Matplotlib, pylab, and pyplot: how are they related?
51+
Matplotlib, pyplot and pylab: how are they related?
5252
====================================================
5353

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`.
5757

58-
Pyplot provides the state-machine interface to the underlying 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::
6462

65-
import matplotlib.pyplot as plt
6663

67-
plt.plot(range(10), range(10))
68-
plt.title("Simple Plot")
69-
plt.show()
64+
import matplotlib.pyplot as plt
65+
import numpy as np
66+
67+
x = np.linspace(0, 2, 100)
68+
69+
plt.plot(x, x, label='linear')
70+
plt.plot(x, x**2, label='quadratic')
71+
plt.plot(x, x**3, label='cubic')
72+
73+
plt.xlabel('x label')
74+
plt.ylabel('y label')
75+
76+
plt.title("Simple Plot")
77+
78+
plt.legend()
7079

71-
Pylab combines the pyplot functionality (for plotting) with the numpy
72-
functionality (for mathematics and for working with arrays)
73-
in a single namespace, making that namespace
74-
(or environment) even more MATLAB-like.
75-
For example, one can call the `sin` and `cos` functions just like
76-
you could in MATLAB, as well as having all the features of pyplot.
80+
plt.show()
7781

78-
The pyplot interface is generally preferred for non-interactive plotting
79-
(i.e., scripting). The pylab interface is convenient for interactive
80-
calculations and plotting, as it minimizes typing. Note that this is
81-
what you get if you use the *ipython* shell with the *-pylab* option,
82-
which imports everything from pylab and makes plotting fully interactive.
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.
93+
94+
For non-interactive plotting it is suggested
95+
to use pyplot to create the figures and then the OO interface for
96+
plotting.
8397

8498
.. _coding_styles:
8599

@@ -99,25 +113,14 @@ The only caveat is to avoid mixing the coding styles for your own code.
99113
Of the different styles, there are two that are officially supported.
100114
Therefore, these are the preferred ways to use matplotlib.
101115

102-
For the preferred pyplot style, the imports at the top of your
116+
For the pyplot style, the imports at the top of your
103117
scripts will typically be::
104118

105119
import matplotlib.pyplot as plt
106120
import numpy as np
107121

108122
Then one calls, for example, np.arange, np.zeros, np.pi, plt.figure,
109-
plt.plot, plt.show, etc. So, a simple example in this style would be::
110-
111-
import matplotlib.pyplot as plt
112-
import numpy as np
113-
x = np.arange(0, 10, 0.2)
114-
y = np.sin(x)
115-
plt.plot(x, y)
116-
plt.show()
117-
118-
Note that this example used pyplot's state-machine to
119-
automatically and implicitly create a figure and an axes. For full
120-
control of your plots and more advanced usage, use the pyplot interface
123+
plt.plot, plt.show, etc. Use the pyplot interface
121124
for creating figures, and then use the object methods for the rest::
122125

123126
import matplotlib.pyplot as plt
@@ -129,22 +132,59 @@ for creating figures, and then use the object methods for the rest::
129132
ax.plot(x, y)
130133
plt.show()
131134

132-
Next, the same example using a pure MATLAB-style::
135+
So, why all the extra typing instead of the MATLAB-style (which relies
136+
on global state and a flat namespace)? For very simple things like
137+
this example, the only advantage is academic: the wordier styles are
138+
more explicit, more clear as to where things come from and what is
139+
going on. For more complicated applications, this explicitness and
140+
clarity becomes increasingly valuable, and the richer and more
141+
complete object-oriented interface will likely make the program easier
142+
to write and maintain.
143+
144+
Typically one finds oneself making the same plots over and over
145+
again, but with different data sets, which leads to needing to write
146+
specialized functions to do the plotting. The recommended function
147+
signature is something like: ::
148+
149+
def my_plotter(ax, data1, data2, param_dict):
150+
"""
151+
A helper function to make a graph
152+
153+
Parameters
154+
----------
155+
ax : Axes
156+
The axes to draw to
157+
158+
data1 : array
159+
The x data
160+
161+
data2 : array
162+
The y data
163+
164+
param_dict : dict
165+
Dictionary of kwargs to pass to ax.plot
166+
167+
Returns
168+
-------
169+
out : list
170+
list of artists added
171+
"""
172+
out = ax.plot(data1, data2, **param_dict)
173+
return out
174+
175+
which you would then use as::
176+
177+
fig, ax = plt.subplots(1, 1)
178+
my_plotter(ax, data1, data2, {'marker':'x'})
133179

134-
from pylab import *
135-
x = arange(0, 10, 0.2)
136-
y = sin(x)
137-
plot(x, y)
138-
show()
180+
or if you wanted to have 2 sub-plots::
139181

182+
fig, (ax1, ax2) = plt.subplots(1, 2)
183+
my_plotter(ax1, data1, data2, {'marker':'x'})
184+
my_plotter(ax2, data3, data4, {'marker':'o'})
140185

141-
So, why all the extra typing as one moves away from the pure
142-
MATLAB-style? For very simple things like this example, the only
143-
advantage is academic: the wordier styles are more explicit, more
144-
clear as to where things come from and what is going on. For more
145-
complicated applications, this explicitness and clarity becomes
146-
increasingly valuable, and the richer and more complete object-oriented
147-
interface will likely make the program easier to write and maintain.
186+
Again, for these simple examples this style seems like overkill, however
187+
once the graphs get slightly more complex it pays off.
148188

149189
.. _what-is-a-backend:
150190

0 commit comments

Comments
 (0)