Package wiat :: Module Plot
[hide private]
[frames] | no frames]

Source Code for Module wiat.Plot

 1  # Copyright 2008 Antoine Lefebvre 
 2  # 
 3  # This file is part of "The Wind Instrument Acoustic Toolkit". 
 4  # 
 5  # "The Wind Instrument Acoustic Toolkit" is free software: 
 6  # you can redistribute it and/or modify it under the terms of 
 7  # the GNU General Public License as published by the 
 8  # Free Software Foundation, either version 3 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # "The Wind Instrument Acoustic Toolkit" is distributed in the hope 
12  # that it will be useful, but WITHOUT ANY WARRANTY; without even 
13  # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
14  # See the GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with "The Wind Instrument Acoustic Toolkit". 
18  # If not, see <http://www.gnu.org/licenses/>. 
19  # 
20  # All rights reserved. 
21   
22  """ Plot.py - Classes for plotting standard impedance plots are defined here. 
23       
24      TODO: cleanup, redesign and maybe elimination. 
25  """ 
26   
27  __all__ = ['ImpedancePlot'] 
28   
29  import numpy 
30  import pylab 
31   
32  LINESTYLES = ('-', '--', '-.', ':', '.', ',', 'o') 
33  CUSTOMDASHES = ((None,None), (5,2), (4,2,1,1), (2,2)) 
34  COLORS = ('blue', 'red', 'green') 
35   
36 -class ImpedancePlot:
37 " Class for generating standard impedance plot with pylab "
38 - def __init__(self, title_string, listtoplot, maximalists=None):
39 self.title_string = title_string 40 self.listtoplot = listtoplot 41 self.maximalists = maximalists 42 self.basefilename = self.title_string.replace(" ","") 43 self.pngfilename = self.basefilename + '.png' 44 self.pdffilename = self.basefilename + '.pdf'
45
46 - def plot(self, style='lin', title=True, fsize=(20,12), xlim=(100,4500)):
47 """ Plot the figure. 48 49 You have to do a pylab.show() to see it. 50 """ 51 self.fig = pylab.figure(figsize=fsize) 52 53 if style is 'dB': 54 func = lambda x: 20*numpy.log10(x) 55 else: 56 func = lambda x: x 57 i = 0 58 for data in self.listtoplot: 59 f,z,lbl = data 60 lines = pylab.semilogx(f, func(numpy.abs(z)), LINESTYLES[i], 61 color=COLORS[i], label=lbl) 62 lines[0].set_dashes(CUSTOMDASHES[i]) 63 i = i+1 64 65 if self.maximalists is not None: 66 for ml in self.maximalists: 67 print ml 68 for f, z in ml: 69 pylab.semilogx([f], func([z]), 'ok') 70 71 if title: 72 pylab.title(self.title_string) 73 74 pylab.xlabel('Frequency [Hz]') 75 if style is 'dB': 76 pylab.ylabel('Normalized impedance (dB): 20log10(Z/Zc)') 77 else: 78 pylab.ylabel('Normalized impedance Z/Zc') 79 pylab.legend() 80 pylab.grid(True) 81 self.fig.axes[0].xaxis.grid(True, which='minor') 82 pylab.xlim(xlim)
83
84 - def save(self, filename=None):
85 """ Save the figure to a file. 86 87 the file name is made from the figure title if not provided. 88 """ 89 if filename is None: 90 filename = self.basefilename 91 print 'saving ' + filename 92 pylab.savefig(filename)
93 94 # def htmlInclude(self,f): 95 # print >>f, "<a href=\"%s\"> Graphic of %s </a>"% 96 #(self.pngfilename,self.descriptionString) 97