Skip to content

Animation

Animator calls a drawing function over a sequence of parameter values and exports the returned Canvas or Figure objects as a GIF. It needs the animation extra (Optional dependencies). Import it from utility_viz.core.animation; it was econ_viz.animation in 1.x.

Animator(draw, frames=values).save(path, fps=12, dpi=120, loop=0)

Call draw(value) for every value in frames and write the canvases as the frames of a GIF.

  • save() uses Pillow, so no ffmpeg dependency is required.
  • Frames are composited onto white before export, which prevents frame-stacking artefacts.
  • fps, dpi and loop set the frame rate, resolution and loop count.
import numpy as np

from utility_viz import Canvas, levels, solve
from utility_viz.core.animation import Animator
from utility_viz.models import CobbDouglas


def draw(px: float) -> Canvas:
    model = CobbDouglas(alpha=0.5, beta=0.5)
    eq = solve(model, px=px, py=2.0, income=20.0)
    lvls = levels.around(eq.utility, n=5)

    return (
        Canvas(
            x_max=14,
            y_max=12,
            x_label="X_1",
            y_label="X_2",
            title="Price sweep",
        )
        .add_utility(model, levels=lvls)
        .add_budget(px=px, py=2.0, income=20.0, fill=True)
        .add_equilibrium(eq, show_ray=True, drop_dashes=True)
    )


Animator(draw, frames=np.linspace(1.0, 6.0, 45)).save(
    "price_sweep.gif",
    fps=12,
    dpi=120,
)

examples/scripts/animation.py provides four examples (Each example covers Cobb-Douglas, CES, perfect substitutes and perfect complements.):

  • Parameter sweep: prices and income fixed, a utility-function parameter varies.
  • Price sweep: utility function and \(p_y\) fixed, \(p_x\) varies and the budget line rotates, as in the example above.
  • Income sweep: utility function and prices fixed, income varies and the budget line shifts.
  • Budget-only sweep: the budget line alone, without the utility layers.

The four differ only in which values draw() holds fixed and which one it varies. For the parameter sweep, let the draw() above take alpha and fix the price at \(p_x = 2\):

def draw(alpha: float) -> Canvas:
    model = CobbDouglas(alpha=alpha, beta=1.0 - alpha)
    eq = solve(model, px=2.0, py=2.0, income=20.0)
    ...


Animator(draw, frames=np.linspace(0.1, 0.9, 45)).save(
    "parameter_sweep.gif"
)

For the income sweep, draw() takes income and passes it to add_budget() and solve(). For the budget line alone, leave out add_utility() and add_equilibrium().

Parameter sweep \(\alpha\) varies with \(\beta = 1 - \alpha\) to Income sweep each show four frames.

import numpy as np

from utility_viz import Canvas, Config, levels, solve
from utility_viz.core.animation import Animator
from utility_viz.models import CobbDouglas

# figure settings
Config.load("econ-viz.toml").use()


def draw(alpha: float) -> Canvas:
    """One frame: the weight on x is alpha, the weight on y is 1 - alpha."""
    model = CobbDouglas(alpha=alpha, beta=1.0 - alpha)
    eq = solve(model, px=2.0, py=2.0, income=20.0)
    cvs = Canvas(
        x_max=14, y_max=12, x_label="$x_1$", y_label="$x_2$"
    )
    cvs.add_utility(
        model, levels=levels.around(eq.utility, n=5)
    )
    cvs.add_budget(2.0, 2.0, 20.0, fill=True)
    cvs.add_equilibrium(eq, show_ray=True)
    return cvs


Animator(draw, frames=np.linspace(0.1, 0.9, 45)).save(
    "parameter_sweep.gif", fps=12, dpi=120
)

Parameter sweep \(\alpha\) varies with \(\beta = 1 - \alpha\)

Parameter sweep \(\alpha\) varies with \(\beta = 1 - \alpha\)

Each figure below shows four frames of its GIF, which has 45.

import numpy as np

from utility_viz import Canvas, Config, levels, solve
from utility_viz.core.animation import Animator
from utility_viz.models import CobbDouglas

# figure settings
Config.load("econ-viz.toml").use()

model = CobbDouglas(alpha=0.5, beta=0.5)
# same curves in every frame
fixed = levels.around(
    solve(model, px=2.0, py=2.0, income=20.0).utility, n=5
)


def draw(px: float) -> Canvas:
    """One frame: the price of x is px."""
    eq = solve(model, px=px, py=2.0, income=20.0)
    cvs = Canvas(
        x_max=14, y_max=12, x_label="$x_1$", y_label="$x_2$"
    )
    cvs.add_utility(model, levels=fixed)
    cvs.add_budget(px, 2.0, 20.0, fill=True)
    cvs.add_equilibrium(eq, show_ray=True)
    return cvs


Animator(draw, frames=np.linspace(1.0, 6.0, 45)).save(
    "price_sweep.gif", fps=12, dpi=120
)

Price sweep

Price sweep

import numpy as np

from utility_viz import Canvas, Config, levels, solve
from utility_viz.core.animation import Animator
from utility_viz.models import CobbDouglas

# figure settings
Config.load("econ-viz.toml").use()

model = CobbDouglas(alpha=0.5, beta=0.5)
# same curves in every frame
fixed = levels.around(
    solve(model, px=2.0, py=2.0, income=20.0).utility, n=5
)


def draw(income: float) -> Canvas:
    """One frame: income is `income`."""
    eq = solve(model, px=2.0, py=2.0, income=income)
    cvs = Canvas(
        x_max=14, y_max=12, x_label="$x_1$", y_label="$x_2$"
    )
    cvs.add_utility(model, levels=fixed)
    cvs.add_budget(2.0, 2.0, income, fill=True)
    cvs.add_equilibrium(eq, show_ray=True)
    return cvs


Animator(draw, frames=np.linspace(8.0, 36.0, 45)).save(
    "income_sweep.gif", fps=12, dpi=120
)

Income sweep

Income sweep

The GIFs below are drawn by scripts/animation_figures.py in the docs repository, with the settings of manual.toml. They cover the same four sweeps: parameter, price, income and budget-only.

Parameter sweeps

Vary utility-function parameters to see how the shape of the preference map changes.

Cobb-Douglas Cobb-Douglas parameter sweep GIF
Vary \(\alpha\) while \(\beta = 1 - \alpha\)
CES CES parameter sweep GIF
Vary \(\rho\) to change curvature and substitutability
Perfect substitutes Perfect substitutes parameter sweep GIF
Vary \(a\) with \(b\) fixed
Leontief Leontief parameter sweep GIF
Vary \(a\) with \(b\) fixed to move the kink path

Price sweeps

Hold the utility function and background indifference-curve levels fixed, then vary one good's price to trace equilibrium as the budget line rotates.

Cobb-Douglas Cobb-Douglas price sweep GIF
Price sweep with \(p_y\) fixed
CES CES price sweep GIF
Price sweep with a fixed utility surface
Perfect substitutes Perfect substitutes price sweep GIF
Under a rotating budget line
Leontief Leontief price sweep GIF
Price sweep with fixed right-angle indifference curves
Budget only Budget-only price sweep GIF
Price sweep for isolating pure rotation of the constraint

Income sweeps

Hold the utility function and prices fixed, then vary income to trace equilibrium as the budget line shifts in parallel.

Cobb-Douglas Cobb-Douglas income sweep GIF
Income sweep with fixed prices
CES CES income sweep GIF
Income sweep with fixed prices and fixed utility function
Perfect substitutes Perfect substitutes income sweep GIF
Income sweep
Leontief Leontief income sweep GIF
Income sweep with fixed prices
Budget only Budget-only income sweep GIF
Income sweep for isolating parallel shifts in the constraint

Comments