Skip to content

Viz

rpps.viz

Visualization helpers. rpps' figures use STYLE (a matplotlib style, default "dark_background"; None: whatever matplotlib is set to); importing this module no longer changes the style of anyone else's figures

Modules:

Name Description
base

Generic viz wrappers

save
utils

base

Generic viz wrappers

Functions:

Name Description
show

Show plots

styled

A context in which new figures and axes get STYLE (nothing when it is None)

figure

Create a new figure

subplot

Create/get a subplot

subplots

Create subplots

ion

Enable interactive mode

ioff

Disable interactive mode

cla

Clear axes

pause

Pause

axes

ax, or the axes of a new figure when it is None (kw: for plt.subplots)

line

Plot y against x and return the line. ax: axes (None: a new figure), or the line an earlier call returned, which is

show

show()

Show plots

Source code in src/rpps/viz/base.py
6
7
8
def show():
    """Show plots"""
    plt.show()

styled

styled()

A context in which new figures and axes get STYLE (nothing when it is None)

Source code in src/rpps/viz/base.py
def styled():
    """A context in which new figures and axes get `STYLE` (nothing when it is None)"""
    import contextlib
    return plt.style.context(STYLE) if STYLE else contextlib.nullcontext()

figure

figure() -> Figure

Create a new figure

Source code in src/rpps/viz/base.py
def figure() -> Figure:
    """Create a new figure"""
    with styled():
        return plt.figure()

subplot

subplot(*args, **kwargs) -> Axes

Create/get a subplot

Source code in src/rpps/viz/base.py
def subplot(*args, **kwargs) -> Axes:
    """Create/get a subplot"""
    with styled():
        return plt.subplot(*args, **kwargs)

subplots

subplots(*args, **kwargs)

Create subplots

Source code in src/rpps/viz/base.py
def subplots(*args, **kwargs):
    """Create subplots"""
    with styled():
        return plt.subplots(*args, **kwargs)

ion

ion()

Enable interactive mode

Source code in src/rpps/viz/base.py
def ion():
    """Enable interactive mode"""
    plt.ion()

ioff

ioff()

Disable interactive mode

Source code in src/rpps/viz/base.py
def ioff():
    """Disable interactive mode"""
    plt.ioff()

cla

cla()

Clear axes

Source code in src/rpps/viz/base.py
def cla():
    """Clear axes"""
    plt.cla()

pause

pause(interval: float)

Pause

Source code in src/rpps/viz/base.py
def pause(interval: float):
    """Pause"""
    plt.pause(interval)

axes

axes(ax=None, **kw) -> Axes

ax, or the axes of a new figure when it is None (kw: for plt.subplots)

Source code in src/rpps/viz/base.py
def axes(ax=None, **kw) -> Axes:
    """ax, or the axes of a new figure when it is None (kw: for plt.subplots)"""
    return subplots(**kw)[1] if ax is None else ax

line

line(ax, x, y, title=None, left=None, right=None, xlabel=None, ylabel=None, grid=None, **style)

Plot y against x and return the line. ax: axes (None: a new figure), or the line an earlier call returned, which is updated in place (an animation). title/left/right: the centre, left and right titles; grid: its alpha

Source code in src/rpps/viz/base.py
def line(ax, x, y, title=None, left=None, right=None, xlabel=None, ylabel=None, grid=None, **style):
    """Plot y against x and return the line. ax: axes (None: a new figure), or the line an earlier call returned, which is
    updated in place (an animation). title/left/right: the centre, left and right titles; grid: its alpha"""
    from matplotlib.lines import Line2D
    if isinstance(ax, Line2D):
        ax.set_data(x, y)
        return ax
    ax = axes(ax)
    ln, = ax.plot(x, y, **style)
    for loc, text in (("center", title), ("left", left), ("right", right)):
        if text is not None:
            ax.set_title(text, loc=loc)
    if xlabel is not None:
        ax.set_xlabel(xlabel)
    if ylabel is not None:
        ax.set_ylabel(ylabel)
    if grid is not None:
        ax.grid(True, alpha=grid)
    ax.set_xlim(x[0], x[-1])
    return ln

save

Classes:

Name Description
FFmpegMP4

with rp.viz.save.FFmpegMP4(fig, fps, "video.mp4") as writer:

FFmpegMP4

FFmpegMP4(fig, fps, path, metadata=None, dpi=200)

with rp.viz.save.FFmpegMP4(fig, fps, "video.mp4") as writer: ... plt.plot() ... writer.grab_frame()

writer = rp.viz.save.FFmpegMP4(fig, fps, "video.mp4") writer.open() ... writer.close()

Source code in src/rpps/viz/save.py
def __init__(self, fig, fps, path, metadata=None, dpi=200):
    self.fig = fig
    self.path = path
    self.dpi = dpi
    if metadata is None:
        metadata = dict(title="Video", artist="Author")

    ImplWriter = manim.writers["ffmpeg"]
    self.writer = ImplWriter(fps=fps, metadata=metadata)
    self.writer.setup(fig, path)

utils