77.. contents ::
88 :backlinks: none
99
10+
11+ .. _general_concepts :
12+
13+ General Concepts
14+ ================
15+
16+ :mod: `matplotlib ` has an extensive codebase that can be daunting to many
17+ new users. However, most of matplotlib can be understood with a fairly
18+ simple conceptual framework and knowledge of a few important points.
19+
20+ All plotting is done by *axes *. In addition to the plots,
21+ the axes objects are responsible for other components that handle axis
22+ labeling, ticks, title, and plot legends. A *figure * is the container
23+ for one or more axes objects.
24+
25+ Everything in matplotlib is organized in a heirarchy. At the top
26+ of the heirarchy is the matplotlib state-machine environment. This
27+ environment is responsible for managing the figures and axes
28+ that have been created and modified by you. The behavior of the matplotlib
29+ environment is similar to MATLAB and therefore should be most familiar to
30+ users with MATLAB experience.
31+
32+ There are two interfaces to this environment: :mod: `pylab ` and
33+ :mod: `matplotlib.pyplot `. Through one of these two interfaces, the user
34+ creates *figures *. These figures, in turn, create one or more *axes *.
35+ These axes are then used for any plotting requested by you. Depending
36+ on how you use matplotlib, these figures and axes can be created explicitly
37+ by you through the interface::
38+
39+ import matplotlib.pyplot as plt
40+
41+ fig = plt.figure()
42+ ax = fig.gca()
43+ ax.plot(range(10), range(10))
44+ ax.set_title("Simple Plot")
45+ plt.show()
46+
47+ or implicitly by the state-machine environment::
48+
49+ import matplotlib.pyplot as plt
50+
51+ plt.plot(range(10), range(10))
52+ plt.title("Simple Plot")
53+ plt.show()
54+
55+
1056.. _pylab :
1157
1258Matplotlib, pylab, and pyplot: how are they related?
1359====================================================
1460
1561Matplotlib 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.
62+ that gets installed alongside :mod: `matplotlib `; and :mod: `matplotlib.pyplot `
63+ is a module in matplotlib.
1964
20- Pyplot provides a MATLAB-style state-machine interface to
21- the underlying object-oriented plotting library in matplotlib.
65+ Pyplot provides a state-machine interface to the underlying plotting
66+ library in matplotlib.
2267For example, calling a plotting function from pyplot will
2368automatically create the necessary figure and axes to achieve
2469the desired plot. Setting a title through pyplot will automatically
@@ -27,7 +72,7 @@ set the title to the current axes object.
2772Pylab combines the pyplot functionality (for plotting) with the numpy
2873functionality (for mathematics and for working with arrays)
2974in a single namespace, making that namespace
30- (or environment) even more MATLAB-like.
75+ (or environment) more MATLAB-like.
3176For example, one can call the `sin ` and `cos ` functions just like
3277you could in MATLAB, as well as having all the features of pyplot.
3378
@@ -37,29 +82,32 @@ plotting in the python shell. Note that this is what you get if you use the
3782*ipython * shell with the *-pylab * option, which imports everything
3883from pylab and makes plotting fully interactive.
3984
40- We have been gradually converting the matplotlib examples
41- from pure MATLAB-style (using "from pylab import \* "), to a preferred
42- style in which pyplot is used for some convenience functions, either
43- pyplot or the object-oriented style is used for the remainder of the
44- plotting code, and numpy is used explicitly for numeric array operations.
85+ .. _coding_styles :
4586
46- In this preferred style, the imports at the top of your script are::
87+ Coding Styles
88+ ==================
4789
48- import matplotlib.pyplot as plt
49- import numpy as np
90+ When viewing this documentation and examples, you will find different
91+ coding styles and usage patterns. These styles are perfectly valid
92+ and have their pros and cons. Just about all of the examples can be
93+ converted into another style and achieve the same results.
94+ The only caveat is to avoid mixing the coding styles for your own code.
5095
51- Then one calls, for example, np.arange, np.zeros, np.pi, plt.figure,
52- plt.plot, plt.show, etc.
96+ .. note ::
97+ Developers for matplotlib have to follow a specific style and guidelines.
98+ See :ref: `developers-guide-index `.
5399
54- Example in pure MATLAB-style::
100+ Of the different styles, there are two that are officially supported.
101+ Therefore, these are the preferred ways to use matplotlib.
55102
56- from pylab import *
57- x = arange(0, 10, 0.2)
58- y = sin(x)
59- plot(x, y)
60- show()
103+ For the preferred pyplot style, the imports at the top of your
104+ scripts will typically be::
61105
62- Now in preferred style, using the pyplot interface::
106+ import matplotlib.pyplot as plt
107+ import numpy as np
108+
109+ Then one calls, for example, np.arange, np.zeros, np.pi, plt.figure,
110+ plt.plot, plt.show, etc. So, a simple example in this style would be::
63111
64112 import matplotlib.pyplot as plt
65113 import numpy as np
@@ -68,8 +116,10 @@ Now in preferred style, using the pyplot interface::
68116 plt.plot(x, y)
69117 plt.show()
70118
71- For full control of your plots and more advanced usage, use the pyplot
72- interface for creating figures, but then use object-orientation for the rest::
119+ Note that this example used pyplot's state-machine to
120+ automatically create a figure and an axes. For full control of
121+ your plots and more advanced usage, use the pyplot interface
122+ for creating figures, and then use the object methods for the rest::
73123
74124 import matplotlib.pyplot as plt
75125 import numpy as np
@@ -80,13 +130,21 @@ interface for creating figures, but then use object-orientation for the rest::
80130 ax.plot(x, y)
81131 plt.show()
82132
83- So, why all the extra typing required as one moves away from the pure
133+ Next, the same example using a pure MATLAB-style::
134+
135+ from pylab import *
136+ x = arange(0, 10, 0.2)
137+ y = sin(x)
138+ plot(x, y)
139+ show()
140+
141+
142+ So, why all the extra typing as one moves away from the pure
84143MATLAB-style? For very simple things like this example, the only
85- advantage is educational : the wordier styles are more explicit, more
144+ advantage is academic : the wordier styles are more explicit, more
86145clear as to where things come from and what is going on. For more
87- complicated applications, the explicitness and clarity become
146+ complicated applications, this explicitness and clarity become
88147increasingly valuable, and the richer and more complete object-oriented
89148interface will likely make the program easier to write and maintain.
90149
91150
92-
0 commit comments