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