Source code for physicskit.nuclear.visualizers

"""Plots and animations for :mod:`physicskit.nuclear`.

- :func:`plot_decay_chain` -- each species' population over time in a
  radioactive decay chain.
- :func:`animate_decay_chain_bars` -- the same populations as an animated
  bar chart.

Every function returns its figure (or animation) rather than calling
``show()``.
"""

from __future__ import annotations

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

__all__ = ["plot_decay_chain", "animate_decay_chain_bars"]


[docs] def plot_decay_chain(t, populations, labels=None, ax=None): """Plot each species' population over time in a radioactive decay chain. Parameters ---------- t : ndarray of shape (n_t,) Times. populations : ndarray of shape (n_species, n_t) Population of each species over time, e.g. from :func:`physicskit.nuclear.radioactivity.bateman_decay_chain`. labels : sequence of str, optional Label for each species; defaults to ``"species 1"``, ``"species 2"``, ... ax : matplotlib.axes.Axes, optional Axes to draw into; a new figure is created if omitted. Returns ------- fig : matplotlib.figure.Figure ax : matplotlib.axes.Axes Examples -------- >>> import numpy as np >>> from physicskit.nuclear.radioactivity import bateman_decay_chain >>> t = np.linspace(0, 10, 50) >>> N = bateman_decay_chain(1000.0, [0.5, 0.2], t) >>> fig, ax = plot_decay_chain(t, N, labels=["Parent", "Daughter"]) >>> isinstance(fig, plt.Figure) True """ populations = np.asarray(populations) if labels is None: labels = [f"species {i + 1}" for i in range(populations.shape[0])] if ax is None: fig, ax = plt.subplots() else: fig = ax.figure for row, label in zip(populations, labels, strict=True): ax.plot(t, row, label=label) ax.set_xlabel("t") ax.set_ylabel("population") ax.legend() return fig, ax
[docs] def animate_decay_chain_bars(t, populations, labels=None, interval=150, ax=None): """Animate each species' population evolving over time in a decay chain. Parameters ---------- t : ndarray of shape (n_t,) Times. populations : ndarray of shape (n_species, n_t) Population of each species over time, e.g. from :func:`physicskit.nuclear.radioactivity.bateman_decay_chain`. labels : sequence of str, optional Bar labels; defaults to ``"species 1"``, ``"species 2"``, ... interval : int, default=150 Delay between frames, in milliseconds. ax : matplotlib.axes.Axes, optional Axes to draw into; a new figure is created if omitted. Returns ------- matplotlib.animation.FuncAnimation Examples -------- >>> import numpy as np >>> from physicskit.nuclear.radioactivity import bateman_decay_chain >>> t = np.linspace(0, 10, 30) >>> N = bateman_decay_chain(1000.0, [0.5, 0.2], t) >>> anim = animate_decay_chain_bars(t, N) >>> anim.__class__.__name__ 'FuncAnimation' """ populations = np.asarray(populations) n_species = populations.shape[0] if labels is None: labels = [f"species {i + 1}" for i in range(n_species)] if ax is None: fig, ax = plt.subplots() else: fig = ax.figure bars = ax.bar(labels, populations[:, 0], color=plt.cm.viridis(np.linspace(0, 1, n_species))) ax.set_ylim(0, populations.max() * 1.05 + 1e-12) ax.set_ylabel("population") title = ax.set_title(f"t = {t[0]:.2f}") def update(frame): for bar, h in zip(bars, populations[:, frame]): bar.set_height(h) title.set_text(f"t = {t[frame]:.2f}") return [*bars, title] return FuncAnimation(fig, update, frames=len(t), interval=interval, blit=False)