Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe55936

Browse files
committed
get data example
svn path=/trunk/matplotlib/; revision=7345
1 parent 3033590 commit fe55936

5 files changed

Lines changed: 70 additions & 1 deletion

File tree

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
12
2009-08-03 Add PathCollection; modify contourf to use complex
23
paths instead of simple paths with cuts. - EF
34

45
2009-08-03 Fixed boilerplate.py so it doesn't break the ReST docs. - JKS
56

7+
2009-07-31 Added cbook.get_mpl_data for urllib enabled fetching and
8+
cacheing of data needed for examples. See
9+
examples/misc/mpl_data_demo.py - JDH
10+
611
======================================================================
712

813
2009-07-31 Tagging 0.99.0.rc1 at 7314 - MGD

examples/misc/mpl_data_demo.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Grab mpl data from the ~/.matplotlib/mpl_data cache if it exists, else
3+
fetch it from svn and cache it
4+
"""
5+
import matplotlib.cbook as cbook
6+
import matplotlib.pyplot as plt
7+
fname = cbook.get_mpl_data('lena.png', asfileobj=False)
8+
9+
print 'fname', fname
10+
im = plt.imread(fname)
11+
plt.imshow(im)
12+
plt.show()

examples/pylab_examples/scatter_demo2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
make a scatter plot with varying color and size arguments
33
"""
4-
import matplotlib
4+
import matplotlib
55
import numpy as np
66
import matplotlib.pyplot as plt
77
import matplotlib.mlab as mlab

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ def _get_data_path_cached():
487487
always=False)
488488

489489

490+
490491
def get_example_data(fname):
491492
"""
492493
return a filehandle to one of the example files in mpl-data/example

lib/matplotlib/cbook.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import numpy.ma as ma
1111
from weakref import ref
1212

13+
import matplotlib
14+
1315
major, minor1, minor2, s, tmp = sys.version_info
1416

1517

@@ -338,6 +340,55 @@ def to_filehandle(fname, flag='rU', return_opened=False):
338340
def is_scalar_or_string(val):
339341
return is_string_like(val) or not iterable(val)
340342

343+
344+
345+
def get_mpl_data(fname, asfileobj=True):
346+
"""
347+
Check the cachedirectory ~/.matplotlib/mpl_data for an mpl_data
348+
file. If it does not exist, fetch it with urllib from the mpl svn repo
349+
350+
http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/mpl_data/
351+
352+
and store it in the cachedir.
353+
354+
If asfileobj is True, a file object will be returned. Else the
355+
path to the file as a string will be returned
356+
357+
To add a datafile to this directory, you need to check out
358+
mpl_data from matplotlib svn::
359+
360+
svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/mpl_data
361+
362+
and svn add the data file you want to support. This is primarily
363+
intended for use in mpl examples that need custom data
364+
"""
365+
366+
# TODO: how to handle stale data in the cache that has been
367+
# updated from svn -- is there a clean http way to get the current
368+
# revision number that will not leave us at the mercy of html
369+
# changes at sf?
370+
371+
372+
configdir = matplotlib.get_configdir()
373+
cachedir = os.path.join(configdir, 'mpl_data')
374+
if not os.path.exists(cachedir):
375+
os.mkdir(cachedir)
376+
377+
cachefile = os.path.join(cachedir, fname)
378+
379+
if not os.path.exists(cachefile):
380+
import urllib
381+
url = 'http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/mpl_data/%s'%urllib.quote(fname)
382+
matplotlib.verbose.report('Attempting to download %s to %s'%(url, cachefile))
383+
urllib.urlretrieve(url, filename=cachefile)
384+
else:
385+
matplotlib.verbose.report('Aleady have mpl_data %s'%fname)
386+
387+
if asfileobj:
388+
return to_filehandle(cachefile)
389+
else:
390+
return cachefile
391+
341392
def flatten(seq, scalarp=is_scalar_or_string):
342393
"""
343394
this generator flattens nested containers such as

0 commit comments

Comments
 (0)