Diffusion Animator¶
The Diffusion Animator visualises a diffusion process as a frame-by-frame
animation of the network graph. Node colours reflect the simulated status at
each iteration: a categorical palette is used for discrete-state models
(e.g. SIR) and a sequential colourmap (RdYlBu_r) for continuous-opinion
models.
- class ndlib.viz.mpl.DiffusionAnimator.DiffusionAnimator(model, iterations, pos=None)¶
- DiffusionAnimator.__init__(model, iterations, pos)¶
- Parameters:
model – A configured and executed NDLib diffusion model.
iterations – Output of
model.iteration_bunch().pos – Optional dict mapping node id → (x, y). If None, a spring layout is computed automatically.
- DiffusionAnimator.plot()¶
Build and return a
matplotlib.animation.FuncAnimationof the diffusion process over the graph.The returned object can be displayed inline in a Jupyter notebook or passed to
save_gif().- Returns:
matplotlib.animation.FuncAnimation
- DiffusionAnimator.save_gif(filename, fps)¶
Save the animation as a GIF file.
- Parameters:
filename – Output path, e.g.
"diffusion.gif".fps – Frames per second (default 10).
Below is a minimal example for the SIR model.
import ndlib.models.ModelConfig as mc
import ndlib.models.epidemics as epd
from ndlib.viz.mpl.DiffusionAnimator import DiffusionAnimator
import networkx as nx
# Network topology
g = nx.erdos_renyi_graph(20, 0.2)
# Model selection
model = epd.SIRModel(g)
# Model configuration
cfg = mc.Configuration()
cfg.add_model_parameter('beta', 0.5)
cfg.add_model_parameter('gamma', 0.1)
cfg.add_model_parameter("fraction_infected", 0.2)
model.set_initial_status(cfg)
# Simulation execution
iterations = model.iteration_bunch(20)
# Animation – returns matplotlib.animation.FuncAnimation
viz = DiffusionAnimator(model, iterations)
anim = viz.plot() # display inline in Jupyter, or …
viz.save_gif("sir.gif", fps=3) # … export as a GIF