@@ -56,21 +56,30 @@ Fernando Perez has provided a nice top level method to create in
5656everything at once, and turn off x and y sharing for the whole bunch.
5757You can either unpack the axes individually::
5858
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)
6161 ax1.plot(x)
6262
6363or get them back as a numrows x numcolumns object array which supports
6464numpy indexing::
6565
66- # new style method 2
66+ # new style method 2; use an axes array
6767 fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
6868 axs[0,0].plot(x)
6969
7070
71+
7172Fixing common date annoyances
7273=============================
7374
75+
76+ .. plot ::
77+ :nofigs:
78+ :context:
79+
80+ # clear the state for context use below
81+ plt.close('all')
82+
7483matplotlib allows you to natively plots python datetime instances, and
7584for the most part does a good job picking tick locations and string
7685formats. 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
97106objects are datetime.date instances, which we can see when we print
98107some samples in the ipython terminal window.
99108
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, ::
102110
103111 In [67]: plot(r.date, r.close)
104112 Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>]
105113
114+ you will see that the x tick labels are all squashed together.
115+
106116.. plot ::
117+ :context:
107118
108119 import matplotlib.cbook as cbook
109120 datafile = cbook.get_sample_data('goog.npy')
@@ -113,23 +124,22 @@ squashed together::
113124 plt.title('Default date handling can cause overlapping labels')
114125
115126Another 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.
126137
127138.. plot ::
128139 :include-source:
140+ :context:
129141
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')
133143 fig, ax = plt.subplots(1)
134144 ax.plot(r.date, r.close)
135145
@@ -140,10 +150,10 @@ im, so we'll use one of those.
140150 # toolbar
141151 import matplotlib.dates as mdates
142152 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')
144154
145155Now 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 .
147157
148158Fill Between and Alpha
149159======================
@@ -154,14 +164,17 @@ illustrating ranges. It has a very handy ``where`` argument to
154164combine filling with logical ranges, eg to just fill in a curve over
155165some threshold value.
156166
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
158168graphs visual appearance. Let's compare two graphs of a financial
159169times with a simple line plot on the left and a filled line on the
160170right.
161171
162172.. plot ::
163173 :include-source:
164174
175+ import matplotlib.pyplot as plt
176+ import numpy as np
177+
165178 import matplotlib.cbook as cbook
166179
167180 # load up some sample financial data
@@ -180,6 +193,9 @@ right.
180193 ax.grid(True)
181194
182195 ax1.set_ylabel('price')
196+ for label in ax2.get_yticklabels():
197+ label.set_visible(False)
198+
183199 fig.suptitle('Google (GOOG) daily closing price')
184200 fig.autofmt_xdate()
185201
@@ -193,21 +209,24 @@ your figures in PNG, PDF or SVG.
193209
194210Our next example computes two populations of random walkers with a
195211different 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
197213standard deviation of the mean position of the population. Here the
198214alpha channel is useful, not just aesthetic.
199215
200216.. plot ::
201217 :include-source:
202218
219+ import matplotlib.pyplot as plt
220+ import numpy as np
221+
203222 Nsteps, Nwalkers = 100, 250
204223 t = np.arange(Nsteps)
205224
206- # an Nsteps x Nwalkers array of random walk steps
225+ # an ( Nsteps x Nwalkers) array of random walk steps
207226 S1 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers)
208227 S2 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
209228
210- # an Nsteps x Nwalkers array of random walker positions
229+ # an ( Nsteps x Nwalkers) array of random walker positions
211230 X1 = S1.cumsum(axis=0)
212231 X2 = S2.cumsum(axis=0)
213232
@@ -232,16 +251,16 @@ alpha channel is useful, not just aesthetic.
232251 ax.grid()
233252
234253
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
241260dashed line, and the plus/minus one sigma deviation from the mean is
242261showsn 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.
245264
246265.. plot ::
247266 :include-source:
@@ -258,7 +277,7 @@ one sigma boundary, and shade that region blue.
258277 S = mu + sigma*np.random.randn(Nsteps)
259278 X = S.cumsum()
260279
261- # the 1 sigma upper and lower population bounds
280+ # the 1 sigma upper and lower analytic population bounds
262281 lower_bound = mu*t - sigma*np.sqrt(t)
263282 upper_bound = mu*t + sigma*np.sqrt(t)
264283
@@ -323,9 +342,9 @@ Placing text boxes
323342When decorating axes with text boxes, two useful tricks are to place
324343the text in axes coordinates (see :ref: `transforms_tutorial `), so the
325344text 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.
329348
330349.. plot ::
331350 :include-source:
0 commit comments