First, I apologize for the long script. It's if anyone wants to see the issue first hand. The problem is that when I use twinx() to plot two different functions on the same axes, the legend from the first part is overwritten by the grid lines of the second part (after the twinx() statement). As you can see in the Plot function, I tried to use the zorder to have the first legend drawn at the end but that doesn't work. I can see two possibilities -

1)  I still don't know how to use zorder, or ...

2) the twinx() statement somehow causes the zorder process to get reset so that the first legend always gets drawn before the second function is plotted.

Thanks for any help,

Wayne
#FilterPlot.py

from pylab import *

pico,micro,meg,gig = 1e-12,1e-6,1000000.,1.E9
NumPts,SaveFile = 2**10,1

def MakeTicks(Low,High,fudge=0):
   K,up,down,Ticks,Inc = 1.,10.,0.1,[],1.
   while (High-Low) <= up:
      High,Low = High*up,Low*up
      K *= up
   while (High-Low) >= up*up:
      High,Low = High*down,Low*down
      K *= down
   if int(fudge):  High += (High-Low)*.05
   Delta,Inc = High-Low,2.
   if Delta > 20.:   Inc = 5.
   if Delta > 55.:   Inc = 10.
   I,inc,Min,Max = 0,Inc/K,Low/K,High/K
   Ticks.append(inc*float(int(Low/Inc)))
   if Ticks[0] > Min:  Ticks[0] -= inc
   while Ticks[-1] <= Max:
      I += 1
      Ticks.append(Ticks[0]+float(I)*inc)
   return Ticks

def Step(Input,df):
   t,v,dt,Out = [],[],1./(2.*NumPts*df),0.
   for I in range(0,len(Input)/4,1):
      t.append(float(I)*dt*1000.)
      Out += Input[I].real
      v.append(Out)                    # integrate impulse response
   return [t,v]

def Impulse(Input,df):
   t,v,dt,Out = [],[],1./(2.*NumPts*df),0.
   for I in range(0,len(Input)/4,1):
      t.append(float(I)*dt*1000.)      # so the time scale is in nanoseconds
      v.append(Input[I].real*1000.)
   return [t,v]

def TransFcn(Rs,C1,L2,C3,L4,C5,Name,Resp=0):
   B = [1.]
   B.append(Rs*(C1+C3+C5))
   B.append(L2*C3+(L4+L2)*C5)
   B.append(Rs*(L2*C1*(C3+C5)+L4*C5*(C1+C3)))
   B.append(L2*L4*C3*C5)
   B.append(Rs*L2*L4*C1*C3*C5)   #for K in range(6):  print 'D['+str(K)+'] = 
',D[K]
   Mag,Arg,Dly,Freq,Xfer,f,unwrap = [],[],[],[],[],0.,0.
   Fmax = round(16.*B[0]/(B[1]*meg),0)    # Want it in MHz (100 nsec -> 160 MHz)
   df = Fmax/float(NumPts)
   while f < Fmax:                        # Gives a sequence length of NumPts
      Freq.append(f)
      w = 2.*pi*f*meg                     # w in radians/sec
      w2 = w*w
      w4 = w2*w2
      Re,Im = B[0]-w2*B[2]+w4*B[4],w*(B[1]-w2*B[3]+w4*B[5])
      cpx = 1./complex(Re,Im)
      mag,arg = abs(cpx),-arctan2(Im,Re)
      Xfer.append(cpx)                    # For fft submission
      Mag.append(mag)
      Arg.append(180./pi*arg)
      Dly.append(gig*((B[1]-3.*w2*B[3]+5.*w4*B[5])*Re \
                     + w*(2.*B[2]-4.*w2*B[4])*Im)/(Re*Re+Im*Im))  # nanoseconds
      f += df
   PlotFile = 'FilterResponse.png'
   Plot(Freq,Xfer,Mag,Dly,Name,Resp,df)
   if SaveFile:
      savefig(PlotFile,dpi=300,facecolor='w',edgecolor='w')

def Plot(Freq,Xfer,Mag,Dly,Name,Resp,df):
   RespType,Which = {0:'Impulse',1:'Step'},{0:Impulse,1:Step}
   K = NumPts/8                           # To expand interesting portion
   Freq,Mag,Dly = Freq[:K],Mag[:K],Dly[:K]
   xLabel,yLabel1,yLabel2 = 'Frequency (MHz)','Magnitude','Delay (nsec)'
   Haxis = MakeTicks(Freq[0],Freq[-1],0)
   PlotTitle = Name + ' Transfer Function'
   PlotList1,PlotList2 = ['Mag'],['Delay']
   figure(figsize=(6,4.5),facecolor='w',dpi=100)
   axes([.095,.09,.80,.85],axisbg='1.0')
   grid(True,ls='-',color=(0.,0.,.65),alpha=0.5,lw=.35)
   plot(Freq,Mag,'b-',lw=1.0)
   Vaxis = MakeTicks(min(Mag),max(Mag),1)
   axis([Haxis[0],Haxis[-1],Vaxis[0],Vaxis[-1]])
   title(PlotTitle)
   ylabel(yLabel1)
   xticks(Haxis)
   yticks(Vaxis)
   l1 = legend(PlotList1,loc=(.76,.92))
   l1.zorder = 99
   m1 = l1.get_frame()
   m1.set_linewidth(0)
   m1.set_facecolor('0.95')
   twinx()
   g2 = grid(True,ls='-',color=(.65,0.,0.),alpha=0.5,lw=.35)
   #g2.zorder = 0   This gives an error
   plot(Freq,Dly,'r-',lw=1.0)
   Vaxis = MakeTicks(min(Dly),max(Dly),1)
   axis([Haxis[0],Haxis[-1],Vaxis[0],Vaxis[-1]])
   xlabel(xLabel)
   ylabel(yLabel2)
   xticks(Haxis)
   yticks(Vaxis)
   l2 = legend(PlotList2,loc=(.76,.86))
   l2.zorder = 100
   m2 = l2.get_frame()
   m2.set_linewidth(0)
   m2.set_facecolor('0.95')

def Main():
   Rs = float(1000)
   C1,C3,C5 = pico*float(6.67),pico*float(31),pico*float(62.3)
   L2,L4 = micro*float(19.5),micro*float(42.1),
   TransFcn(Rs,C1,L2,C3,L4,C5,'Bessel',1)
   show()

try:
   Main()
finally:
   print '\'FilterPlot.py\' is finished.'
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to