Package PyDSTool :: Module matplotlib_import
[hide private]
[frames] | no frames]

Source Code for Module PyDSTool.matplotlib_import

 1  """
 
 2      Plotting imports for PyDSTool, from Matplotlib's pyplot library.
 
 3  
 
 4      Robert Clewley, March 2006.
 
 5  """ 
 6  
 
 7  from numpy import Inf, NaN, isfinite, int, int8, int16, int32, int64, float, float32, float64 
 8  import matplotlib 
 9  ver = matplotlib.__version__.split(".") 
10  try: 
11      if int(ver[0]) == 0 and int(ver[1]) < 65: 
12          import matplotlib.matlab as plt 
13          from matplotlib.matlab import * 
14      else: 
15          import matplotlib.pyplot as plt 
16          from matplotlib.pyplot import * 
17  except RuntimeError, err: 
18      if str(err) == 'could not open display': 
19          failed=True 
20      else: 
21          raise 
22  else: 
23      failed=False 
24  
 
25  if failed: 
26      # Dummy plot overrides for PyDSTool when matplotlib fails to import
 
27  
 
28 - def plot(*args, **kw):
29 print "Warning: plot does not work!"
30
31 - def save_fig(fignum, fname, formats=[]):
32 print "Warning: plot does not work!"
33 34 print "Warning: matplotlib failed to import properly and so is not" 35 print " providing a graphing interface" 36 plt = None # will cause an error if someone tries to access in order to plot 37 else: 38 import os 39 from Trajectory import Trajectory 40 from common import _num_types 41 42 # Convenient shorthand to permit singleton numeric types and Trajectories 43 # in the plot arguments without first converting them to lists or arrays.
44 - def plot(*args, **kw):
45 new_args = list(args) 46 if isinstance(args[0], _num_types): 47 new_args[0] = [args[0]] 48 elif isinstance(args[0], Trajectory): 49 try: 50 new_args[0] = args[0].sample() 51 except: 52 raise RuntimeError("Could not sample trajectory with default " 53 "options for plotting") 54 if len(args) > 1: 55 if isinstance(args[1], _num_types): 56 new_args[1] = [args[1]] 57 elif isinstance(args[1], Trajectory): 58 try: 59 new_args[1] = args[1].sample() 60 except: 61 raise RuntimeError("Could not sample trajectory with " 62 "default options for plotting") 63 return plt.plot(*tuple(new_args), **kw)
64
65 - def save_fig(fignum, fname, formats=['png','svg','eps']):
66 """Save figure fignum to multiple files with different formats 67 and extensions given by the formats argument. 68 These are platform-dependent and are specific to matplotlib's support. 69 """ 70 for f in formats: 71 plt.figure(fignum).savefig(fname+'.'+f)
72