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

Source Code for Module PyFoam.Basics.MatplotlibTimelines

  1  #  ICE Revision: $Id$ 
  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 platform 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.xlabel="" 56 self.ylabel="" 57 self.ylabel2="" 58 try: 59 if self.spec.xlabel: 60 self.setXLabel(self.spec.xlabel) 61 except AttributeError: 62 pass 63 try: 64 if self.spec.ylabel: 65 self.setYLabel(self.spec.ylabel) 66 except AttributeError: 67 pass 68 try: 69 if self.spec.y2label: 70 self.setYLabel2(self.spec.y2label) 71 except AttributeError: 72 pass 73 74 self.axis1=None 75 self.axis2=None 76 77 self.setTitle(self.spec.theTitle) 78 79 self.with_=self.spec.with_ 80 if not self.with_ in ['lines','points','dots','steps','linespoints']: 81 warning("'with'-style",self.with_,"not implemented, using 'lines'") 82 self.with_='lines' 83 self.redo()
84
85 - def buildData(self,times,name,title,lastValid):
86 """Build the implementation specific data 87 @param times: The vector of times for which data exists 88 @param name: the name under which the data is stored in the timeline 89 @param title: the title under which this will be displayed""" 90 91 a=self.axis1 92 if self.testAlternate(name): 93 a=self.axis2 94 data=self.data.getValues(name) 95 tm=times 96 if len(tm)>0 and not lastValid: 97 tm=tm[:-1] 98 data=data[:-1] 99 plotIt=True 100 try: 101 if self.spec.logscale and min(data)<=0: 102 plotIt=False 103 except AttributeError: 104 pass 105 106 if self.spec.start!=None or self.spec.end!=None: 107 start=self.spec.start 108 end=self.spec.end 109 if start==None: 110 start=tm[0] 111 if end==None: 112 end=tm[-1] 113 self.axis1.set_xbound(lower=start,upper=end) 114 self.axis1.set_autoscalex_on(False) 115 if self.axis2: 116 self.axis2.set_xbound(lower=start,upper=end) 117 self.axis2.set_autoscalex_on(False) 118 119 drawstyle='default' 120 marker='' 121 linestyle='-' 122 123 if self.with_=='lines': 124 pass 125 elif self.with_=='steps': 126 drawstyle='steps' 127 elif self.with_=='points': 128 linestyle='' 129 marker='*' 130 elif self.with_=='dots': 131 linestyle='' 132 marker='.' 133 elif self.with_=='linespoints': 134 marker='*' 135 else: 136 warning("'with'-style",self.with_,"not implemented, using 'lines'") 137 138 if plotIt: 139 a.plot(tm, 140 data, 141 label=title, 142 drawstyle=drawstyle, 143 marker=marker, 144 linestyle=linestyle)
145
146 - def preparePlot(self):
147 """Prepare the plotting window""" 148 plt.hot() 149 self.figure=plt.figure(self.figNr) 150 self.figure.clear() 151 # this is black magic that makes the legend work with two axes 152 if self.hasSubplotHost: 153 self.axis1=SubplotHost(self.figure,111) 154 self.figure.add_subplot(self.axis1) 155 else: 156 self.axis1=self.figure.add_subplot(111) 157 self.axis1.set_xlabel(self.xlabel) 158 self.axis1.set_ylabel(self.ylabel) 159 160 if len(self.alternate)>0: 161 self.axis2=self.axis1.twinx() 162 self.axis2.set_ylabel(self.ylabel2) 163 164 try: 165 if self.spec.logscale: 166 self.axis1.set_yscale("log") 167 if self.axis2: 168 self.axis2.set_yscale("log") 169 except AttributeError: 170 pass
171
172 - def doReplot(self):
173 """Replot the whole data""" 174 175 if self.hasSubplotHost: 176 l=self.axis1.legend(fancybox=True) 177 else: 178 l=plt.legend(fancybox=True) 179 # l.get_frame().set_fill(False) 180 if l: 181 l.get_frame().set_alpha(0.7) 182 l.get_texts()[0].set_size(10) 183 plt.suptitle(self.title) 184 plt.grid(True) 185 plt.draw()
186 # plt.show() 187
188 - def actualSetTitle(self,title):
189 """Sets the title""" 190 191 self.title=title
192
193 - def setXLabel(self,title):
194 """Sets the label on the X-Axis""" 195 196 self.xlabel=title
197
198 - def setYLabel(self,title):
199 """Sets the label on the first Y-Axis""" 200 201 self.ylabel=title
202
203 - def setYLabel2(self,title):
204 """Sets the label on the second Y-Axis""" 205 206 self.ylabel2=title
207
208 - def doHardcopy(self,filename,form):
209 """Write the contents of the plot to disk 210 @param filename: Name of the file without type extension 211 @param form: String describing the format""" 212 213 self.figure.savefig(filename+"."+form,format=form)
214 215 # Should work with Python3 and Python2 216