@@ -56,21 +56,30 @@ Fernando Perez has provided a nice top level method to create in
56
56
everything at once, and turn off x and y sharing for the whole bunch.
57
57
You can either unpack the axes individually::
58
58
59
- # new style method 1
60
- fig, (ax1, ax2, ax3, ax4) = plt.subplots(2, 2, sharex=True, sharey=True)
59
+ # new style method 1; unpack the axes
60
+ fig, (( ax1, ax2), ( ax3, ax4) ) = plt.subplots(2, 2, sharex=True, sharey=True)
61
61
ax1.plot(x)
62
62
63
63
or get them back as a numrows x numcolumns object array which supports
64
64
numpy indexing::
65
65
66
- # new style method 2
66
+ # new style method 2; use an axes array
67
67
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
68
68
axs[0,0].plot(x)
69
69
70
70
71
+
71
72
Fixing common date annoyances
72
73
=============================
73
74
75
+
76
+ .. plot ::
77
+ :nofigs:
78
+ :context:
79
+
80
+ # clear the state for context use below
81
+ plt.close('all')
82
+
74
83
matplotlib allows you to natively plots python datetime instances, and
75
84
for the most part does a good job picking tick locations and string
76
85
formats. There are a couple of things it does not handle so
@@ -97,13 +106,15 @@ which means it is a 4-byte python object pointer; in this case the
97
106
objects are datetime.date instances, which we can see when we print
98
107
some samples in the ipython terminal window.
99
108
100
- If you plot the data, you will see that the x tick labels are all
101
- squashed together::
109
+ If you plot the data, ::
102
110
103
111
In [67]: plot(r.date, r.close)
104
112
Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>]
105
113
114
+ you will see that the x tick labels are all squashed together.
115
+
106
116
.. plot ::
117
+ :context:
107
118
108
119
import matplotlib.cbook as cbook
109
120
datafile = cbook.get_sample_data('goog.npy')
@@ -113,23 +124,22 @@ squashed together::
113
124
plt.title('Default date handling can cause overlapping labels')
114
125
115
126
Another annoyance is that if you hover the mouse over a the window and
116
- look in the lower right corner of the matplotlib toolbar at the x and
117
- y coordinates, you see that the x locations are formatted the same way
118
- the tick labels are, eg "Dec 2004". What we'd like is for the
119
- location in the toolbar to have a higher degree of precision, eg
120
- giving us the exact date out mouse is hovering over. To fix the first
121
- problem, we can use method:` matplotlib.figure.Figure.autofmt_xdate() `
122
- and to fix the second problem we can use the `` ax.fmt_xdata ``
123
- attribute which can be set to any function that takes a position and
124
- returns a string. matplotlib has a number of date formatters built
125
- im , so we'll use one of those.
127
+ look in the lower right corner of the matplotlib toolbar
128
+ ( :ref: ` navigation-toolbar `) at the x and y coordinates, you see that
129
+ the x locations are formatted the same way the tick labels are, eg
130
+ "Dec 2004". What we'd like is for the location in the toolbar to have
131
+ a higher degree of precision, eg giving us the exact date out mouse is
132
+ hovering over. To fix the first problem, we can use
133
+ method:` matplotlib.figure.Figure.autofmt_xdate ` and to fix the second
134
+ problem we can use the `` ax.fmt_xdata `` attribute which can be set to
135
+ any function that takes a scalar and returns a string. matplotlib has
136
+ a number of date formatters built in , so we'll use one of those.
126
137
127
138
.. plot ::
128
139
:include-source:
140
+ :context:
129
141
130
- import matplotlib.cbook as cbook
131
- datafile = cbook.get_sample_data('goog.npy')
132
- r = np.load(datafile).view(np.recarray)
142
+ plt.close('all')
133
143
fig, ax = plt.subplots(1)
134
144
ax.plot(r.date, r.close)
135
145
@@ -140,10 +150,10 @@ im, so we'll use one of those.
140
150
# toolbar
141
151
import matplotlib.dates as mdates
142
152
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
143
- plt.title('autfmt_xdate fixes the labels')
153
+ plt.title('fig.autofmt_xdate fixes the labels')
144
154
145
155
Now when you hover your mouse over the plotted data, you'll see date
146
- format strings like 2004-12-01.
156
+ format strings like 2004-12-01 in the toolbar .
147
157
148
158
Fill Between and Alpha
149
159
======================
@@ -154,14 +164,17 @@ illustrating ranges. It has a very handy ``where`` argument to
154
164
combine filling with logical ranges, eg to just fill in a curve over
155
165
some threshold value.
156
166
157
- At it's most basic level, ``fill_between `` can be use to enhance a
167
+ At its most basic level, ``fill_between `` can be use to enhance a
158
168
graphs visual appearance. Let's compare two graphs of a financial
159
169
times with a simple line plot on the left and a filled line on the
160
170
right.
161
171
162
172
.. plot ::
163
173
:include-source:
164
174
175
+ import matplotlib.pyplot as plt
176
+ import numpy as np
177
+
165
178
import matplotlib.cbook as cbook
166
179
167
180
# load up some sample financial data
@@ -180,6 +193,9 @@ right.
180
193
ax.grid(True)
181
194
182
195
ax1.set_ylabel('price')
196
+ for label in ax2.get_yticklabels():
197
+ label.set_visible(False)
198
+
183
199
fig.suptitle('Google (GOOG) daily closing price')
184
200
fig.autofmt_xdate()
185
201
@@ -193,21 +209,24 @@ your figures in PNG, PDF or SVG.
193
209
194
210
Our next example computes two populations of random walkers with a
195
211
different mean and standard deviation of the normal distributions from
196
- which there steps are drawn. We use shared regions to plot +/- one
212
+ which the steps are drawn. We use shared regions to plot +/- one
197
213
standard deviation of the mean position of the population. Here the
198
214
alpha channel is useful, not just aesthetic.
199
215
200
216
.. plot ::
201
217
:include-source:
202
218
219
+ import matplotlib.pyplot as plt
220
+ import numpy as np
221
+
203
222
Nsteps, Nwalkers = 100, 250
204
223
t = np.arange(Nsteps)
205
224
206
- # an Nsteps x Nwalkers array of random walk steps
225
+ # an ( Nsteps x Nwalkers) array of random walk steps
207
226
S1 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers)
208
227
S2 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
209
228
210
- # an Nsteps x Nwalkers array of random walker positions
229
+ # an ( Nsteps x Nwalkers) array of random walker positions
211
230
X1 = S1.cumsum(axis=0)
212
231
X2 = S2.cumsum(axis=0)
213
232
@@ -232,16 +251,16 @@ alpha channel is useful, not just aesthetic.
232
251
ax.grid()
233
252
234
253
235
- The where keyword argument is very handy for highlighting certain
236
- regions of the graph. Where takes a boolean mask the same length as
237
- the x, ymin and ymax arguments, and only fills in the region where the
238
- boolean mask is True. In the example below, we take a a single random
239
- walker and compute the analytic mean and standard deviation of the
240
- population positions. The population mean is shown as the black
254
+ The `` where `` keyword argument is very handy for highlighting certain
255
+ regions of the graph. `` where `` takes a boolean mask the same length
256
+ as the x, ymin and ymax arguments, and only fills in the region where
257
+ the boolean mask is True. In the example below, we simulate a single
258
+ random walker and compute the analytic mean and standard deviation of
259
+ the population positions. The population mean is shown as the black
241
260
dashed line, and the plus/minus one sigma deviation from the mean is
242
261
showsn as the yellow filled region. We use the where mask
243
- ``X>upper_bound `` to find the region where the walker is above the
244
- one sigma boundary, and shade that region blue.
262
+ ``X>upper_bound `` to find the region where the walker is above the one
263
+ sigma boundary, and shade that region blue.
245
264
246
265
.. plot ::
247
266
:include-source:
@@ -258,7 +277,7 @@ one sigma boundary, and shade that region blue.
258
277
S = mu + sigma*np.random.randn(Nsteps)
259
278
X = S.cumsum()
260
279
261
- # the 1 sigma upper and lower population bounds
280
+ # the 1 sigma upper and lower analytic population bounds
262
281
lower_bound = mu*t - sigma*np.sqrt(t)
263
282
upper_bound = mu*t + sigma*np.sqrt(t)
264
283
@@ -323,9 +342,9 @@ Placing text boxes
323
342
When decorating axes with text boxes, two useful tricks are to place
324
343
the text in axes coordinates (see :ref: `transforms_tutorial `), so the
325
344
text doesn't move around with changes in x or y limits. You can also
326
- use the bbox property of text to surround the text with a
327
- :class: `~matplotlib.patches.Patch ` instance -- the boox keyword argument
328
- takes a dictionary with keys that are Patch properties.
345
+ use the `` bbox `` property of text to surround the text with a
346
+ :class: `~matplotlib.patches.Patch ` instance -- the `` bbox `` keyword
347
+ argument takes a dictionary with keys that are Patch properties.
329
348
330
349
.. plot ::
331
350
:include-source:
0 commit comments