codex/flow-cytometer-pulse-width.py

flow-cytometer-pulse-width.py
"""Illustrative equal-height flow-cytometer pulses; Python 3.14, NumPy 2.3.5, Matplotlib 3.10.7."""
from pathlib import Path
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

def pulse(t, sigma):
    return np.exp(-0.5 * (np.asarray(t) / sigma) ** 2)

def main():
    t = np.linspace(-7, 7, 1401)
    fig, ax = plt.subplots(figsize=(8, 4.2), dpi=100, facecolor='white')
    ax.set_facecolor('white')
    for sigma, color in [(2, '#da7626'), (1, '#215caa')]:
        y = pulse(t, sigma)
        ax.fill_between(t, y, color=color, alpha=0.13)
        ax.plot(t, y, color=color, lw=2.3,
                label=rf'$\sigma={sigma}$; ideal area ${sigma}\sqrt{{2\pi}}$')
    ax.set(xlim=(-7, 7), ylim=(0, 1.14), xlabel='Time relative to pulse center (arbitrary units)',
           ylabel='Signal intensity (arbitrary units)', title='Same pulse height, different integrated signal')
    ax.axhline(1, color='#666666', ls=':', lw=1)
    ax.text(-6.6, 1.035, 'Both peak at 1', color='#444444', fontsize=10)
    ax.legend(loc='upper right', framealpha=1)
    ax.grid(alpha=0.15)
    fig.text(0.5, 0.015, 'Original Gaussian model; ideal infinite-window area ratio 2:1, not experimental data.',
             ha='center', fontsize=9)
    fig.tight_layout(rect=(0, 0.035, 1, 1))
    fig.savefig(Path('flow-cytometer-pulse-width.png'), facecolor='white')
    plt.close(fig)

if __name__ == '__main__':
    main()