Visualization · Animation
Animation and interactive views¶
Add .animate() to a plot. Export HTML when you want to rotate an orbit or
inspect values in a browser; save MP4 or GIF when you need a recording for a
presentation.
Page previews play while visible and repeat. Pause any player to keep it still. Reduced-motion preferences start all motion paused.
Interact in your browser¶
The same plot can become an interactive HTML file instead of a recording. Choose Threejs to rotate a 3-D path while it plays. Choose Plotly to hover over values, zoom into a time interval, and inspect the revealed data. Neither needs a Python kernel after export.
Rotate the orbit · Threejs¶
Drag to orbit, scroll or pinch to zoom, and use the lower-left controls to pause or restart the reveal. The camera stays under your control while the path moves. Axes are hidden in this preview to give the geometry room; the trajectory still contains the original state coordinates and time samples.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=30, dt=0.01)
web_plot = ts.plot(trajectory, color="#2CC5AE").theme("dark")
web_plot.style(axes=False).camera(elev=24, azim=-60)
web_plot.animate(duration=16).trail(350, backdrop_alpha=0.12)
web_plot.head(size=7, color="#E8912D")
# skip-doctest — writes an interactive HTML file
web_plot.save("orbit.html", backend="threejs", axes=False)
Inspect the time trace · Plotly¶
Pause, then hover to read a sample's time and value. Drag a rectangle to zoom; double-click to restore the full interval. The top-right toolbar also provides pan and reset controls. Restart returns the reveal to its first sample without discarding your chosen axis range.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=30, dt=0.02)
web_plot = ts.plot(
trajectory, "time_series", components="x", color="#2CC5AE"
).theme("dark").font(family="sans-serif", size=12)
web_plot.relabel(title="Inspect a revealed time series", x="Time", y="x")
web_plot.animate(duration=16).trail(None).head(size=7, color="#E8912D")
# skip-doctest — writes an interactive HTML file; requires tsdynamics[plotly]
web_plot.save("trace.html", backend="plotly")
These are real exports from the examples above, with a static loading/failure image added to the documentation's Plotly embed. They load a pinned renderer from a CDN, so opening the HTML files requires an internet connection. A static image remains available if scripts, WebGL, or the CDN are unavailable. Playback pauses outside the visible page and in hidden tabs; an explicit pause stays in effect when you return. The Threejs preview uses thin WebGL lines and a hard trail. Use the Matplotlib movies below for fading trails, scripted camera spin, animated layouts, or evolving spatial fields.
Your first movie¶
Install the optional drawing backend with pip install "tsdynamics[viz]".
For MP4, install FFmpeg on your path; on macOS, brew install ffmpeg.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=30, dt=0.01)
movie = ts.plot(trajectory, color="#2CC5AE").theme("dark")
movie.animate(duration=10, fps=24)
# skip-doctest — writes a movie using the external FFmpeg executable
movie.save("lorenz.mp4", size=(960, 600), dpi=120)
The output grid (dt=0.01) controls the recorded data. Playback duration and
frame rate control how those samples are shown. More movie frames do not make
the simulation more accurate.
Leave a trail¶
Keep the whole path with .trail(None), or keep the most recent samples with
.trail(300). A fading trail makes recent motion stand out. Use .copy() when
you want independent versions of the same plot.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=30, dt=0.01)
base = ts.plot(trajectory, components=["x", "z"], color="#2CC5AE").theme("dark")
persistent = base.copy().relabel(title="Keep the path").trail(None)
window = base.copy().relabel(title="Last 300 samples").trail(300, backdrop=False)
fading = base.copy().relabel(title="Fade those 300").trail(
300, fade=True, backdrop=False
)
movie = (persistent | window | fading).font(family="sans-serif", size=10)
movie.animate(duration=12, fps=24)
To specify a physical duration instead, use .trail(("time", 3)) on uniformly
sampled data. .trail(("steps", 300)) is the explicit spelling of .trail(300).
By default, a short trail has a faint full-path backdrop; tune it with
.trail(backdrop=True, backdrop_alpha=0.10). Turn it off for a reveal that should
not show the ending early.
Move the camera¶
.camera(elev=..., azim=...) sets a 3-D viewing angle in degrees. Add spin to
turn the camera over one forward pass: negative turns reverse the direction,
zero holds the view still, and positive turns move it the other way.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=30, dt=0.01)
base = ts.plot(trajectory, color="#2CC5AE").theme("dark").style(axes=False)
base.trail(250, fade=True, backdrop_alpha=0.12).head(size=7, color="#E8912D")
reverse = base.copy().relabel(title="spin = -0.25").camera(
elev=24, azim=-60, spin=-0.25
)
fixed = base.copy().relabel(title="spin = 0").camera(
elev=24, azim=-60, spin=0
)
forward = base.copy().relabel(title="spin = +0.25").camera(
elev=24, azim=-60, spin=0.25
)
movie = (reverse | fixed | forward).font(family="sans-serif", size=10)
movie.animate(duration=12, fps=24)
Set the individual panels' cameras before composing them. A later
movie.camera(spin=0.25) applies that spin to the whole figure. Camera motion
changes the view, not the trajectory.
Overlay two orbits¶
Use + when the coordinates and units match. Here two nearby Lorenz starts
share one state-space panel, with distinct colours and a visible legend.
import tsdynamics as ts
model = ts.systems.Lorenz()
a = model.run(final_time=30, dt=0.01, ic=[1, 1, 1])
b = model.run(final_time=30, dt=0.01, ic=[1.005, 1, 1])
first = ts.plot(
a, components=["x", "z"], labels=["x₀ = 1.000"], color="#2CC5AE"
)
second = ts.plot(
b, components=["x", "z"], labels=["x₀ = 1.005"], color="#E8912D"
)
movie = (first + second).theme("dark").font(family="sans-serif")
movie.animate(duration=10, fps=24).trail(250, fade=True, backdrop_alpha=0.10)
movie.head(size=7, symbol="s").clock("t = {t:.1f}")
Use | or / when axes represent different quantities. A spectrum and a phase
portrait belong in separate panels, even if their colours match.
Link the views¶
A row is just portrait | trace. Animate the composed figure once; both panels
advance along the same recorded trajectory.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=30, dt=0.01)
portrait = ts.plot(
trajectory, components=["x", "z"], title="State space", color="#2CC5AE"
)
trace = ts.plot(
trajectory, "time_series", components="x", title="x over time", color="#2CC5AE"
)
movie = (portrait | trace).theme("dark").font(family="sans-serif").size(9, 4)
movie.animate(duration=12, fps=24).trail(("time", 4), fade=True)
Inputs with different lengths follow their own relative progress over the same playback window. That is not a guarantee of equal physical times: use the same trajectory or a common time grid when that relationship matters.
Nest the layout¶
Put a row above a timeline with (front | side) / trace. The bottom panel spans
the full width; each panel keeps its own axes.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=30, dt=0.01)
front = ts.plot(
trajectory, components=["x", "z"], title="Front: x, z", color="#2CC5AE"
)
side = ts.plot(
trajectory, components=["y", "z"], title="Side: y, z", color="#E8912D"
)
trace = ts.plot(
trajectory, "time_series", components="x",
title="One shared timeline", color="#2CC5AE"
)
movie = ((front | side) / trace).theme("dark").font(family="sans-serif")
movie.animate(duration=12, fps=24).trail(300, fade=True, backdrop_alpha=0.10)
Tweaks on the figure can apply to all panels. Select a child for a local change:
movie[0][1] is the upper-right projection; movie[1] is the lower trace.
See composition for grids and shared axes.
Show map iterations¶
A map gives ordered iterates, not a continuously traversed curve. Draw them as points and keep the iteration label explicit.
import tsdynamics as ts
iterates = ts.systems.Henon(ic=[0, 0]).run(steps=600)
movie = ts.plot(
iterates, "phase_portrait", components=["x", "y"],
primitive="points", color="#2CC5AE"
)
movie.theme("dark").font(family="sans-serif").style(markersize=2)
movie.animate(n_frames=240, fps=24).trail(None).head(False)
movie.clock("n = {t:.0f} · frame {i}")
For maps, use sample-count trails such as .trail(100). The movie may reveal
several stored iterates per output frame; its frame rate is not a dynamical
integration step.
Play a spatial field¶
A field movie replaces the whole spatial state each frame. Select the field by name, then animate the same plot; the trajectory carries its grid metadata.
import tsdynamics as ts
model = ts.systems.GrayScott(N=48, F=0.06, k=0.062, seed=0)
trajectory = model.run(final_time=6000, dt=25)
movie = ts.plot(trajectory, "spatial_field", components="v", cmap="magma")
movie.theme("dark").font(family="sans-serif").relabel(title="Gray–Scott activator v")
movie.animate(duration=10, fps=24).clock("t = {t:.0f}")
A one-dimensional field uses a changing profile line instead of a heatmap. Keep a fixed colour range when comparing field frames. Trails, moving heads and camera spin describe orbit reveals; they do not turn a heatmap into a moving particle. Pattern formation depends on the model, parameters, grid and time window, so this example states all four.
Play forward and back¶
pingpong=True adds a reverse pass. This is reverse playback of recorded
samples; it does not solve the dynamics backward.
import tsdynamics as ts
trajectory = ts.systems.Lorenz(ic=[1, 1, 1]).run(final_time=12, dt=0.02)
movie = ts.plot(trajectory, components=["x", "z"], color="#2CC5AE")
movie.theme("dark").font(family="sans-serif")
movie.animate(duration=5, fps=24, pingpong=True, loop=False)
movie.trail(100, fade=True, backdrop=False).head(color="#E8912D", size=8)
movie.clock("t = {t:.1f}")
MP4 stores that one sequence. The video player's repeat setting controls whether
it starts again; .animate(loop=False) controls repeat behavior in animation
players that support it. GIF playback timing is quantized, so an arbitrary
requested frame rate may be approximated.
All the controls¶
Start with the recipes above, then adjust the same movie object. Fluent calls
mutate and return the plot; movie.copy() gives an independent version.
| Control | Meaning | Visible example |
|---|---|---|
.animate(duration=10, fps=24) |
Forward-pass playback seconds and encoded frames per second | Every recipe |
.animate(n_frames=240, fps=24) |
Exact forward frame count; overrides duration |
Map iterations |
.animate(loop=False) |
Repetition in players that support it; MP4 repetition belongs to its player | Forward and back |
.animate(pingpong=True) |
Append a reverse pass without repeating its endpoints | Forward and back |
.animate(mode="reveal") |
Progressively show recorded orbit samples; the default | First movie |
.animate(mode="frames") |
Play a stored spatial-field stack; detected automatically for field data | Spatial field |
.trail(None) |
Keep the whole revealed path | First movie, maps |
.trail(300) or .trail(("steps", 300)) |
Keep 300 stored samples | Trail comparison |
.trail(("time", 4)) |
Keep four physical time units on a uniform time grid | Linked views |
.trail(fade=True) |
Fade older tail segments | Trail comparison |
.trail(backdrop=False) |
Hide the faint full-path context | Trail comparison |
.trail(backdrop_alpha=0.10) |
Set the full-path context opacity | Overlay |
.head(False) |
Hide the current-state marker | Maps |
.head(size=8, color="#E8912D", symbol="o") |
Set marker size, colour and shape | Amber circles in ping-pong; square heads in the overlay |
.camera(elev=24, azim=-60) |
Fixed 3-D angles in degrees | Camera comparison |
.camera(spin=-0.25) / 0 / 0.25 |
Reverse / fixed / forward quarter-turn per forward pass | Camera comparison |
.clock("t = {t:.1f}") |
Live time or iteration readout; .clock(False) hides it |
Overlay, maps |
.clock("frame {i}: t = {t:.1f}") |
Include the playback-frame index | Map iterations |
A shorter recording can repeat sample positions to fill an explicit frame count.
A smoother recording needs a finer output grid, not just a larger movie frame
count. For ping-pong, duration and n_frames describe the forward pass; the
return leg adds max(n_frames - 2, 0) frames.
Customize the figure, then export it¶
Themes, axes, annotations and layout remain ordinary plotting controls. Add
.style(linewidth=0.9), .font(family="sans-serif", size=11), .background(...),
.limits(...), .gridlines(...) or .camera(...) to the plot you already made.
The camera comparison uses .style(axes=False) deliberately; keep axes visible
for figures whose units or scales matter.
# A separate presentation variant: the earlier movie keeps its settings.
presentation = movie.copy().theme("publication").font(family="sans-serif", size=11)
presentation.head(size=7, color="#D64562", symbol="s")
# skip-doctest — exports files with optional drawing tools
presentation.save("talk.mp4", size=(1600, 960), dpi=160)
presentation.save("talk.gif", size=(800, 480), dpi=100)
size= on .save() is in pixels. .size(width, height) on a plot is in inches;
dpi= determines their conversion. MP4 needs FFmpeg. GIF uses Pillow and needs
no FFmpeg, but usually produces a larger file with fewer colours. Choose a solid
background for either movie format.
Choose the output deliberately¶
| Output | Animation support |
|---|---|
| Matplotlib → MP4 / GIF | All recipes here, including fading trails, signed spin, clocks, overlays, nested layouts and field stacks |
| Plotly → HTML | Supported single-panel reveals; animated composites and spatial-field movies are refused rather than saved as partial output |
| three.js → HTML / geometry | Supported single-panel line/point reveals; no fading tails, camera spin, clock text or arbitrary marker shapes |
| PNG / SVG / PDF | A still of the final animation frame |
| JSON | Serializable plot data and animation settings; no encoded video |
Use the backend guide for exact fallback and export rules.
For a supported single-panel interactive view, choose
movie.save("orbit.html", backend="threejs") or
movie.save("trace.html", backend="plotly"). An animated composite belongs in
MP4 or GIF. This page starts previews only while they are visible, repeats them,
and retains your explicit pause. A downloaded MP4's repeating behavior is chosen
by its video player. Use the player's fullscreen control when comparing small
panels.