Updating Dr. Berendsen's Python programs

(6) Contour plot of 2

To (5)      To invcum(1)      To Workbench Python

Updating Python Code 7.3 (p.184) for Figure 7.5 (p.103)
<code7.3.py>

For two fitting parameters the chi squared is given by
      2=ijBijij
Figure 7.5 is the result for B=[[1,-1],[-1,4]].
In his code7.3.py Dr. Berendsen uses his original 'contour' for obtaining points on the contour. He says that 'autoplotp' will do the plotting task but it cannot be located.
With the popular PyPlotLib I could get the picture in the right. A primary change to his code7.3.py is that 'xlist, ylist' are returned instead of the single 'data'. The plotting program is shown below.

  1. def contour:
  2. ......iOriginal code 7.3 with modificationsj
  3. def fxy(x,y):
  4.    v = np.array([[x,y]])
  5.    w = np.dot(v, matrx @ v.T)
  6.    return w
  7. plt.xlim(-5., 5.)
  8. plt.ylim(-3., 3.)
  9. xycenter= np.array([0.0, 0.0])
  10. for i in range(1,6):
  11.    z = i
  12.    xlist,ylist = contour(fxy,z,xycenter)
  13. plt.plot(xlist,ylist,color='blue')
  14. font1 = {'family':'serif','color':'blue','size':11}
  15. font2 = {'family':'serif','color':'darkred','size':12}
  16. plt.title(r"$\Delta \chi^2$ contours (Fig.7.5)", fontdict = font1)
  17. plt.xlabel(r"$\Delta \theta_1$", fontdict = font2)
  18. plt.ylabel(r"$\Delta \theta_2$", fontdict = font2)
  19. plt.grid()
  20. plt.show()
<Graphics output>

<Remark>

For fxy(x,y)=v B vT, v is a row vector, [x,y], and vTis a column vector. If the matrix B is specified, it is possible to expand fxy to obtain
    fxy=x2-2xy+4y2
but matrix computation is adopted in the left for the purpose of excercise.

The modifications to the original code 7.3 are as folows

  • 'import numpy as np' is added
  • 'import matplotlib.pyplot as plt' is added
  • 'return data' is replaced by 'return xlist,ylist'
<Another method using matplotlib.contourf>

Plotting is included. v  B vT is calculated for the vector v.

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. matrx = np.array([[1.0, -1.0],[-1.0, 4.0]])/3.0
  4. nxpt = 201; nypt = 121
  5. x = np.linspace(-5., 5.,nxpt)
  6. y = np.linspace(-3., 3.,nypt)
  7. z = np.zeros((nypt,nxpt))
  8. xx, yy = np.meshgrid(x,y)
  9. def findz(x,y):
  10.    for i in range(nypt):
  11.      yi = y[i,0]
  12.      for j in range(nxpt):
  13.        xj = x[0,j]
  14.        v = np.array([xj,yi])
  15.        w = matrx @ v.T
  16.        z[i,j] = np.dot(v, w)
  17.    return z
  18. zz = findz(xx, yy)
  19. # zz = (xx**2 - 2.*xx*yy + 4.*yy**2)/3.0
  20. xycenter= np.array([0.0, 0.0])
  21. figure = plt.contourf(xx,yy,zz,levels=[1.,2.,3.,4.,5.],\
  22.    colors=['#FF6666','#FFB266','#FFFF66','#B2FF66', \
  23.    '#66FF66','#66FFFF'])
  24. font1 = {'family':'serif','color':'blue','size':11}
  25. font2 = {'family':'serif','color':'darkred','size':12}
  26. plt.title(r"$\Delta \chi^2$ contours (Fig.7.5)", fontdict = font1)
  27. plt.xlabel(r"$\Delta \theta_1$", fontdict = font2)
  28. plt.ylabel(r"$\Delta \theta_2$", fontdict = font2)
  29. plt.grid()
  30. plt.show()
<Graphics output>

<Remark>

Moving of line 19 to line 18 changes the matrix calculation to polynomial calculation.

9-18-2023, S. Hayashi