Plotting¶
The plotting functions live in epydemix.visualization. They are built on matplotlib:
each one accepts an optional ax to draw on, and returns the axes so you can keep
customising the figure.
from epydemix.visualization import plot_quantiles, plot_trajectories
Function |
Shows |
|---|---|
median and interval over time, optionally with observed data |
|
every individual run |
|
posterior of one parameter |
|
joint posterior of two parameters |
|
distances of the accepted simulations |
|
population by demographic group |
|
contact matrix of one layer |
|
intensity of contacts over time, with interventions |
Simulation results¶
from epydemix import load_predefined_model
model = load_predefined_model("SIR")
results = model.run_simulations(start_date="2024-01-01", end_date="2024-04-10", Nsim=100)
# Median and 90% interval
plot_quantiles(
results.get_quantiles_compartments(),
columns=["Infected_total", "Recovered_total"],
labels=["Infected", "Recovered"],
ylabel="People",
)
# Every run as a thin line
plot_trajectories(
results.get_stacked_compartments(),
columns="Infected_total",
dates=results.dates,
)
The interval drawn by plot_quantiles is set by lower_q and upper_q (5% and 95% by
default), which must be among the quantiles in the table you pass. To overlay observed
data, pass a DataFrame as data with a date column and a column named like the
variable you plot, and set show_data=True, as in Calibration.
Several plots in one figure¶
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(11, 4))
plot_quantiles(results.get_quantiles_compartments(), columns="Infected_total", ax=axes[0], title="Infected")
plot_quantiles(results.get_quantiles_transitions(), columns="Susceptible_to_Infected_total", ax=axes[1], title="New infections")
Other plots¶
Populations and contact matrices: see Populations and contacts.
Contact intensity over time: see Interventions.
Posterior distributions and model fit: see Calibration.
For plots the built-in functions don’t cover, get the data out as a table with
results.to_dataframe() (see Running simulations) and use matplotlib or seaborn directly.