4.2. Interactive visualization of propagation of light in Jupyter notebookΒΆ

Matplotlib present useful visualization tools for showing the intensity profiles at a distance z in an interactive fashion in jupyter notebooks. Here, we show how to use it.

In the first place, we well determine the intensity distribution XZ for an object.

[1]:
from diffractio import degrees, mm, plt, sp, um, np
from diffractio.scalar_sources_X import Scalar_source_X
from diffractio.scalar_masks_X import Scalar_mask_X
from diffractio.scalar_masks_XZ import Scalar_mask_XZ
from numpy.fft import fft, ifft
from scipy.signal import convolve2d

%matplotlib inline
from ipywidgets import interactive

# size of figures
import matplotlib
matplotlib.rcParams['figure.figsize']=(5,5)
matplotlib.rcParams['figure.dpi']=125
number of processors: 8
[2]:
# Initial parameters
x0 = np.linspace(-1050 * um, 1050 * um, 1024*2)
z0 = np.linspace(-0.125*um, 4 * mm, 1024*4)
wavelength = 0.6238 * um*10
[3]:
# Definition of source
u0 = Scalar_source_X(x=x0, wavelength=wavelength)
u0.gauss_beam(A=1, x0=0 * um, z0=0 * um, w0=2000 * um, theta=0. * degrees)
[4]:
# insert sphere
u1 = Scalar_mask_XZ(x=x0, z=z0, wavelength=wavelength, n_background=1)

u1.sphere(r0=(0,1.25*mm), radius=(1*mm,1*mm),refraction_index=1.33, angle=0)
u1.draw_refraction_index(scale='scaled', colorbar_kind='horizontal');
../../../_images/source_tutorial_drawing_use_interact_visualize_profiles_5_0.png
[5]:
#loading the incident field in the simulation
u1.incident_field(u0)
[6]:
# WPM propagation
u1.WPM(verbose=False)
[7]:
#simple drawing
u1.draw(logarithm=True, scale='scaled', draw_borders=True);
../../../_images/source_tutorial_drawing_use_interact_visualize_profiles_8_0.png
[8]:
#interactive function
#from matplotlib.lines import Line2D
intensity=abs(u1.u)**2
Imax=intensity.max()
[9]:
def __interact2__(i):
    plt.plot(u1.x,abs(intensity[:,i]),'k')
    plt.title("z = {:2.3f} mm".format(u1.z[i]/1000))
    plt.ylim(0, Imax)
    plt.xlim(-100*um,100*um)
[10]:
interactive(__interact2__, i=(0,len(u1.z)-1,1))

title

[ ]: