Sunday 25 August 2013

Code for TrA animation

As promised the code for the animation I did previously which shows how transient absorption data is collected.
So I import the libraries and generate the mock data. I then play around with the plot so it looks right.

import numpy as np
import matplotlib.pyplot as plt
from scipy import special
from mpl_toolkits.mplot3d import axes3d
import numpy.ma as ma

#Generate some mock data
t1 = np.linspace(-10,-2,num=9)
t = np.append(t1,np.linspace(-1,1,num=9))
t = np.append(t,np.linspace(2,10,num=9))
T = np.append(t,np.linspace(20,100,num=9))
L = np.linspace(400,800)

l,t = np.meshgrid(L,T)

#Spectra
A = np.exp(-(l-650)**2/(2*50**2))+0.3*np.exp(-(l-550)**2/(2*50**2))

A1 = 0.01
T1 = 20.0
mu = 0.0
w = 0.2
y0 = 0

d = (w/(2*np.sqrt(2*np.log(2))))
#Kinetics
K = A1*1/2*np.exp(-t/T1)*np.exp((mu+(d**2)/(2*T1))/T1)*(1+special.erf((t-(mu+(d**2)/T1))/(np.sqrt(2)*d))) + y0

#Masking matrix
mask = np.ones(l.shape)

#2D spectrum
Psi = K*A

#Forming the initial plot
fig = plt.figure(figsize=(13,8))
ax = fig.add_subplot(221, projection='3d')
ax.plot_wireframe(l, t, Psi, linewidth=0.5)
ax.view_init(35, 340)
ax.set_zlim(0.0,0.012)

plt.subplot(222)
plt.contourf(L,T,Psi,500)
plt.colorbar()
plt.title("Contour plot")
plt.xlabel("Wavelength (nm)")
plt.ylabel("Time (ps)")

plt.subplot(223)
plt.plot(T,Psi)
plt.title("Kinetic")
plt.xlabel("Time (ps)")
plt.ylabel("Abs.")

plt.subplot(224)
plt.plot(L,Psi.T)
plt.title("Spectra")
plt.xlabel("Wavelength (nm)")
plt.ylabel("Abs.")

plt.subplots_adjust(left=0.06, bottom=0.06, right=0.99, top=0.96, wspace=0.15, hspace=0.22)
plt.savefig("animation.png")
plt.show()

Then I loop through the masking matrix and save each plot successively.

for i in range(10,L.shape[0]):
    mask_i = mask.copy()
    mask_i[i:,:] = 0.0
    Psi_i = Psi.copy()*mask_i
    
    fig = plt.figure(figsize=(13,8))
    ax = fig.add_subplot(221, projection='3d')
    ax.plot_wireframe(l, t, Psi_i,linewidth=0.5)
    ax.view_init(35, 340)
    ax.set_zlim(0.0,0.012)
    
    plt.subplot(222)
    plt.contourf(L,T,Psi_i,200)
    plt.colorbar()
    plt.title("Contour plot")
    plt.xlabel("Wavelength (nm)")
    plt.ylabel("Time (ps)")

    plt.subplot(223)
    plt.plot(T,Psi_i)
    plt.title("Kinetic")
    plt.xlabel("Time (ps)")
    plt.ylabel("Abs.")
    plt.ylim(0.0,0.012)

    plt.subplot(224)
    plt.plot(L,Psi_i.T)
    plt.title("Spectra")
    plt.xlabel("Wavelength (nm)")
    plt.ylabel("Abs.")
    plt.ylim(0.0,0.012)

    plt.subplots_adjust(left=0.06, bottom=0.06, right=0.99, top=0.96, wspace=0.15, hspace=0.22)
    plt.savefig("animation_%s.png"%(i))

I used ImageJ to put the pictures into a gif and set the time between the frames.

No comments:

Post a Comment