Skip to content

CLASS Output

Complete workflow: configure CLASS, compute, wrap with ClassOutput, plot all observables.

Optional dependency

This example requires the classy Python package:

pip install cplots[classy]
The code blocks below show the actual calls verbatim but are not executed during the docs build because classy is an optional dependency.


Setup

from classy import Class
import cplots

cosmo = Class()
cosmo.set({
    "h": 0.6736,
    "Omega_b": 0.02237,
    "Omega_cdm": 0.1200,
    "A_s": 2.100e-9,
    "n_s": 0.9649,
    "tau_reio": 0.0544,
    "output": "tCl pCl lCl mPk",
    "lensing": "yes",
    "P_k_max_h/Mpc": 1.0,
    "z_max_pk": 2.0,
    "l_max_scalars": 2500,
})
cosmo.compute()

out = cplots.ClassOutput(cosmo)

Background evolution

bg = out.background()
cplots.BackgroundEvolution(bg).show()

Plot only H(z) and luminosity distance:

bg = out.background(
    quantities=["H [1/Mpc]", "lum. dist."],
    labels={"lum. dist.": r"$d_L$ [Mpc]"},
)
cplots.BackgroundEvolution(bg).show()

Matter power spectrum

Linear P(k) at z=0 in h/Mpc units:

pk = out.pk(z=0.0, k_min=1e-4, k_max=1.0, n_k=300)
cplots.PKSpectrum(pk).show()

Multiple redshifts on the same axes:

import cplots

fig = cplots.Figure()
for z in [0.0, 0.5, 1.0, 2.0]:
    pk = out.pk(z=z, n_k=300)
    fig.add(cplots.PKSpectrum(pk, label=f"z={z}"))
fig.show()

CMB angular power spectra

TT, EE, and TE spectra with the standard ℓ(ℓ+1)/2π scaling:

cls = out.cl(spectra=["tt", "ee", "te"], lensed=True, ell_factor=True)
cplots.ClSpectrum(cls).show()

Unlensed TT only, in K²:

cls = out.cl(spectra=["tt"], lensed=False, units="K2")
cplots.ClSpectrum(cls).show()

Primordial power spectrum

prim = out.primordial()
cplots.PKSpectrum(prim).show()

Thermodynamics

Visibility function against redshift:

thermo = out.thermodynamics()
cplots.BackgroundEvolution(thermo).show()

Multiple thermodynamic quantities:

thermo = out.thermodynamics(quantities=["g [Mpc^-1]", "Tb [K]"])
cplots.BackgroundEvolution(thermo).show()

Cleanup

Always free CLASS memory when done:

cosmo.struct_cleanup()
cosmo.empty()