Package PyFoam :: Package Basics :: Module MatplotlibTimelines
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.MatplotlibTimelines

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Basics/MatplotlibTimelines.py 5768 2009-10-31T12:43:14.986952Z bgschaid  $  
  2  """Plots a collection of timelines""" 
  3   
  4  from PyFoam.Error import warning,error 
  5   
  6  from PyFoam.Basics.CustomPlotInfo import readCustomPlotInfo,CustomPlotInfo 
  7   
  8  from GeneralPlotTimelines import GeneralPlotTimelines 
  9   
 10  from os import uname 
 11   
 12  firstTimeImport=True 
 13   
14 -class MatplotlibTimelines(GeneralPlotTimelines):
15 """This class opens a matplotlib window and plots a timelines-collection in it""" 16 17 figureNr=1 18
19 - def __init__(self, 20 timelines, 21 custom, 22 showWindow=True, 23 registry=None):
24 """@param timelines: The timelines object 25 @type timelines: TimeLineCollection 26 @param custom: A CustomplotInfo-object. Values in this object usually override the 27 other options 28 """ 29 30 self.hasSubplotHost=True 31 try: 32 global plt,matplotlib,firstTimeImport,SubplotHost 33 import matplotlib 34 if not showWindow and firstTimeImport: 35 matplotlib.use("agg") 36 firstTimeImport=False 37 import matplotlib.pyplot as plt 38 try: 39 from mpl_toolkits.axes_grid.parasite_axes import SubplotHost 40 except ImportError: 41 self.hasSubplotHost=False 42 warning("Matplotlib-Version does not support SubplotHost") 43 except ImportError: 44 error("Matplotlib not installed.") 45 46 GeneralPlotTimelines.__init__(self,timelines,custom,showWindow=showWindow,registry=registry) 47 48 self.figNr=MatplotlibTimelines.figureNr 49 MatplotlibTimelines.figureNr+=1 50 51 self.figure=None 52 self.title="" 53 54 self.ylabel="" 55 self.ylabel2="" 56 try: 57 if self.spec.ylabel: 58 self.setYLabel(self.spec.ylabel) 59 except AttributeError: 60 pass 61 try: 62 if self.spec.y2label: 63 self.setYLabel2(self.spec.y2label) 64 except AttributeError: 65 pass 66 67 self.axis1=None 68 self.axis2=None 69 70 self.setTitle(self.spec.theTitle) 71 72 self.with_=self.spec.with_ 73 if not self.with_ in ['lines','points','dots','steps','linespoints']: 74 warning("'with'-style",self.with_,"not implemented, using 'lines'") 75 self.with_='lines' 76 self.redo()
77
78 - def buildData(self,times,name,title,lastValid):
79 """Build the implementation specific data 80 @param times: The vector of times for which data exists 81 @param name: the name under which the data is stored in the timeline 82 @param title: the title under which this will be displayed""" 83 84 a=self.axis1 85 if name in self.alternate: 86 a=self.axis2 87 data=self.data.getValues(name) 88 tm=times 89 if len(tm)>0 and not lastValid: 90 tm=tm[:-1] 91 data=data[:-1] 92 plotIt=True 93 try: 94 if self.spec.logscale and min(data)<=0: 95 plotIt=False 96 except AttributeError: 97 pass 98 99 drawstyle='default' 100 marker='' 101 linestyle='-' 102 103 if self.with_=='lines': 104 pass 105 elif self.with_=='steps': 106 drawstyle='steps' 107 elif self.with_=='points': 108 linestyle='' 109 marker='*' 110 elif self.with_=='dots': 111 linestyle='' 112 marker='.' 113 elif self.with_=='linespoints': 114 marker='*' 115 else: 116 warning("'with'-style",self.with_,"not implemented, using 'lines'") 117 118 if plotIt: 119 a.plot(tm, 120 data, 121 label=title, 122 drawstyle=drawstyle, 123 marker=marker, 124 linestyle=linestyle)
125
126 - def preparePlot(self):
127 """Prepare the plotting window""" 128 plt.hot() 129 self.figure=plt.figure(self.figNr) 130 self.figure.clear() 131 # this is black magic that makes the legend work with two axes 132 if self.hasSubplotHost: 133 self.axis1=SubplotHost(self.figure,111) 134 self.figure.add_subplot(self.axis1) 135 else: 136 self.axis1=self.figure.add_subplot(111) 137 self.axis1.set_xlabel("Time") 138 self.axis1.set_ylabel(self.ylabel) 139 if self.spec.start or self.spec.end: 140 self.axis1.set_xbound(lower=self.spec.start,upper=self.spec.end) 141 142 if len(self.alternate)>0: 143 self.axis2=self.axis1.twinx() 144 self.axis2.set_ylabel(self.ylabel2) 145 146 try: 147 if self.spec.logscale: 148 self.axis1.set_yscale("log") 149 if self.axis2: 150 self.axis2.set_yscale("log") 151 except AttributeError: 152 pass
153
154 - def doReplot(self):
155 """Replot the whole data""" 156 157 if self.hasSubplotHost: 158 l=self.axis1.legend(fancybox=True) 159 else: 160 l=plt.legend(fancybox=True) 161 # l.get_frame().set_fill(False) 162 if l: 163 l.get_frame().set_alpha(0.7) 164 l.get_texts()[0].set_size(10) 165 plt.suptitle(self.title) 166 plt.draw()
167
168 - def actualSetTitle(self,title):
169 """Sets the title""" 170 171 self.title=title
172
173 - def setYLabel(self,title):
174 """Sets the label on the first Y-Axis""" 175 176 self.ylabel=title
177
178 - def setYLabel2(self,title):
179 """Sets the label on the second Y-Axis""" 180 181 self.ylabel2=title
182
183 - def doHardcopy(self,filename,form):
184 """Write the contents of the plot to disk 185 @param filename: Name of the file without type extension 186 @param form: String describing the format""" 187 188 self.figure.savefig(filename+"."+form,format=form)
189