Skip to content

Commit e3cdce8

Browse files
committed
fix a bug in context so plot directive will be forced to run (but not save) figs which are already cached
svn path=/branches/v1_0_maint/; revision=8785
1 parent 84e0589 commit e3cdce8

File tree

7 files changed

+76
-110
lines changed

7 files changed

+76
-110
lines changed

doc/users/annotations_guide.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ more control, it supports a few other options.
310310
as 2. The callable object should take a single argument of
311311
renderer instance. For example, following two commands give
312312
identical results ::
313+
313314
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1,
314315
xytext=(30,0), textcoords="offset points")
315316
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1.get_window_extent,
@@ -322,7 +323,7 @@ more control, it supports a few other options.
322323
annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction"))
323324

324325
0.5 is in data coordinate, and 1 is in normalized axes coordinate.
325-
You may use an atist or transform as with a tuple. For example,
326+
You may use an atist or transform as with a tuple. For example,
326327

327328
.. plot:: users/plotting/examples/annotate_simple_coord02.py
328329
:include-source:

doc/users/gridspec.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _gridspec-guide:
1+
\.. _gridspec-guide:
22

33

44
************************************************
@@ -23,7 +23,7 @@
2323
====================================
2424

2525
To use subplot2grid, you provide geometry of the grid and the location
26-
of the subplot in the grid. For a simple single-cell subplot, ::
26+
of the subplot in the grid. For a simple single-cell subplot::
2727

2828
ax = plt.subplot2grid((2,2),(0, 0))
2929

doc/users/image_tutorial.rst

+4-9
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ examples, if you use the -pylab method, you can skip the "mpimg." and
4040
Importing image data into Numpy arrays
4141
===============================================
4242

43-
Plotting image data is supported by the Python Image Library (`PIL
44-
<http://www.pythonware.com/products/pil/>`_), . Natively, matplotlib
43+
Plotting image data is supported by the Python Image Library (`PIL <http://www.pythonware.com/products/pil/>`_), . Natively, matplotlib
4544
only supports PNG images. The commands shown below fall back on PIL
4645
if the native read fails.
4746

@@ -122,8 +121,7 @@ reading/writing for any format other than PNG is limited to uint8
122121
data. Why 8 bits? Most displays can only render 8 bits per channel
123122
worth of color gradation. Why can they only render 8 bits/channel?
124123
Because that's about all the human eye can see. More here (from a
125-
photography standpoint): `Luminous Landscape bit depth tutorial
126-
<http://www.luminous-landscape.com/tutorials/bit-depth.shtml>`_.
124+
photography standpoint): `Luminous Landscape bit depth tutorial <http://www.luminous-landscape.com/tutorials/bit-depth.shtml>`_.
127125

128126
Each inner list represents a pixel. Here, with an RGB image, there
129127
are 3 values. Since it's a black and white image, R, G, and B are all
@@ -179,8 +177,7 @@ channel of our data:
179177

180178
In [6]: lum_img = img[:,:,0]
181179

182-
This is array slicing. You can read more in the `Numpy tutorial
183-
<http://www.scipy.org/Tentative_NumPy_Tutorial>`_.
180+
This is array slicing. You can read more in the `Numpy tutorial <http://www.scipy.org/Tentative_NumPy_Tutorial>`_.
184181

185182
.. sourcecode:: ipython
186183

@@ -229,9 +226,7 @@ object:
229226
imgplot = plt.imshow(lum_img)
230227
imgplot.set_cmap('spectral')
231228

232-
There are many other colormap schemes available. See the `list and
233-
images of the colormaps
234-
<http://matplotlib.sourceforge.net/examples/pylab_examples/show_colormaps.html>`_.
229+
There are many other colormap schemes available. See the `list and images of the colormaps <http://matplotlib.sourceforge.net/examples/pylab_examples/show_colormaps.html>`_.
235230

236231
.. _Color Bars
237232

doc/users/recipes.rst

+55-36
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,30 @@ Fernando Perez has provided a nice top level method to create in
5656
everything at once, and turn off x and y sharing for the whole bunch.
5757
You 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

6363
or get them back as a numrows x numcolumns object array which supports
6464
numpy 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+
7172
Fixing 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+
7483
matplotlib allows you to natively plots python datetime instances, and
7584
for the most part does a good job picking tick locations and string
7685
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
97106
objects are datetime.date instances, which we can see when we print
98107
some 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

115126
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.
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

145155
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.
147157

148158
Fill Between and Alpha
149159
======================
@@ -154,14 +164,17 @@ illustrating ranges. It has a very handy ``where`` argument to
154164
combine filling with logical ranges, eg to just fill in a curve over
155165
some 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
158168
graphs visual appearance. Let's compare two graphs of a financial
159169
times with a simple line plot on the left and a filled line on the
160170
right.
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

194210
Our next example computes two populations of random walkers with a
195211
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
197213
standard deviation of the mean position of the population. Here the
198214
alpha 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
241260
dashed line, and the plus/minus one sigma deviation from the mean is
242261
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.
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
323342
When decorating axes with text boxes, two useful tricks are to place
324343
the text in axes coordinates (see :ref:`transforms_tutorial`), so the
325344
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.
329348

330349
.. plot::
331350
:include-source:

examples/units/date_converter.py

-20
This file was deleted.

examples/units/date_support.py

-36
This file was deleted.

lib/matplotlib/sphinxext/plot_directive.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def run_code(plot_path, function_name, plot_code, context=False):
210210

211211
if plot_code is not None:
212212
exec_code = 'import numpy as np; import matplotlib.pyplot as plt\n%s'%plot_code
213+
#print 'CONTEXT', context, plot_context, exec_code
213214
if context:
214215
exec(exec_code, None, plot_context)
215216
else:
@@ -279,6 +280,8 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
279280
basedir, fname = os.path.split(plot_path)
280281
basename, ext = os.path.splitext(fname)
281282

283+
284+
282285
all_exists = True
283286

284287
# Look for single-figure output files first
@@ -288,7 +291,7 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
288291
all_exists = False
289292
break
290293

291-
if all_exists:
294+
if not context and all_exists:
292295
return 1
293296

294297
# Then look for multi-figure output files, assuming
@@ -307,7 +310,7 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
307310
else:
308311
break
309312

310-
if i != 0:
313+
if not context and i != 0:
311314
return i
312315

313316
# We didn't find the files, so build them
@@ -321,12 +324,16 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
321324
warnings.warn(s, PlotWarning)
322325
return 0
323326

324-
num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats)
327+
if not all_exists:
328+
num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats)
329+
330+
if '__plot__' in sys.modules:
331+
del sys.modules['__plot__']
325332

326-
if '__plot__' in sys.modules:
327-
del sys.modules['__plot__']
333+
return num_figs
334+
else:
335+
return 1
328336

329-
return num_figs
330337

331338
def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
332339
options, state_machine):

0 commit comments

Comments
 (0)