codex/pulse-shape-energy-comparison.py

pulse-shape-energy-comparison.py
"""Generate pulse-shape-energy-comparison.png in cwd.

Tested with Python 3.14.4, NumPy and Matplotlib 3.10.7.
Run from _media; dependencies are recorded in the top-level pyproject.toml.
"""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

t=np.linspace(-3,3,1201)
fig,axes=plt.subplots(1,2,figsize=(7,3.2),dpi=100,layout='constrained')
ax=axes[0]
ax.plot(t,np.exp(-2*np.log(2)*np.abs(t)),color='#2166ac',label='Exponential',linewidth=2)
ax.plot([-3,-.5,-.5,.5,.5,3],[0,0,1,1,0,0],color='#b35806',label='Rectangle',linewidth=1.8)
ax.axhline(.5,color='gray',linestyle=':',linewidth=1)
ax.plot([-.5,.5],[.5,.5],'o',color='black',markersize=3)
ax.set(xlim=(-3,3),ylim=(-.02,1.08),xlabel=r'Time / FWHM ($t/\tau$)',ylabel=r'$P(t)/P_{\rm pk}$',title='Same peak and FWHM')
ax.legend(fontsize=8,loc='upper right')
ax.text(-2.9,.15,'Exponential tails continue',fontsize=8)
ax.grid(alpha=.15)
ax=axes[1]
values=[1,1/np.log(2)]
bars=ax.bar(['Rectangle','Exponential'],values,color=['#b35806','#2166ac'],width=.6)
for bar,value in zip(bars,values):ax.text(bar.get_x()+bar.get_width()/2,value+.04,f'{value:.4f}',ha='center',fontsize=10)
ax.set(ylim=(0,1.7),ylabel=r'$U/(P_{\rm pk}\tau)$',title='Different total energies')
ax.grid(axis='y',alpha=.15)
ax.set_axisbelow(True)
fig.savefig('pulse-shape-energy-comparison.png',facecolor='white',dpi=100)
plt.close(fig)