ipyvolume

Orbit real 3D point clouds, volumes, and surfaces in a Shiny app, with the camera itself as a reactive input.

ipyvolume is a WebGL-based 3D plotting library for Jupyter: real-time volume rendering of 3D arrays, GPU-instanced scatter/quiver plots, surface and mesh plotting, and frame-by-frame animation with a client-side Play control. With @render_ipyvolume, orbiting the figure updates camera(), a reactive accessor that reports the new position back to Python the same way input.my_slider() does.

hero

A numerically integrated Lorenz attractor, colored by local speed. Try the app.

Installation

pip install 'shinywidgets[ipyvolume]'

The ipyvolume extra installs everything the renderer needs.

Quick start

The pylab API is the fastest way to get a figure on screen: draw with ipv.quickscatter() (or ipv.scatter()/ipv.figure()), then return the current figure – here, the bundled iris dataset in 3D, colored by species:

import ipyvolume as ipv
import numpy as np
import plotly.express as px
from shiny.express import ui

from shinywidgets import render_ipyvolume

iris = px.data.iris()
# Per-point color must be a numeric (N, 3) RGB array in [0, 1] -- a per-point
# array of color *strings* (e.g. "tomato") hangs ipyvolume's serializer; see
# the "Gotchas" section.
palette = np.array([[0.91, 0.30, 0.24], [0.18, 0.49, 0.20], [0.21, 0.45, 0.69]])
color = palette[iris["species_id"] - 1]

ui.page_opts(title="ipyvolume quickstart", fillable=True)


@render_ipyvolume
def plot():
    ipv.quickscatter(
        iris["sepal_length"],
        iris["petal_length"],
        iris["sepal_width"],
        size=1.2,
        marker="sphere",
        color=color,
    )
    return ipv.gcf()

quickstart

Overview

A look at what you can build. Click any of them to open the app, with its code, in Shinylive.

ipv.volshow()/ipv.quickvolshow(), ipv.quiver(), and ipv.plot_surface()/ipv.plot_wireframe() work the same way as the quick start – call the pylab function, then return ipv.gcf().

Animation controls

ipv.animation_control() turns a Scatter whose arrays have an extra leading “frame” axis into an animated comet, with a client-side Play/stop/loop control. The control is a separate widget: add=False returns the Play/slider HBox for you to render as a second output, instead of the default add=True, which drops it into the pylab container – invisible if your render function returns the bare Figure.

import ipyvolume as ipv
import numpy as np
from shiny.express import ui

from shinywidgets import render_ipyvolume, render_widget

ui.page_opts(title="Animation", fillable=True)

# A helix traced out over time: shape (frames, points, 3). The first axis is
# the animation sequence -- get this axis order wrong and the animation plays
# the wrong dimension.
t = np.linspace(0, 4 * np.pi, 300)
helix = np.stack([np.cos(t), np.sin(t), t / 4], axis=1)
frames = np.stack([np.roll(helix, -i, axis=0)[:40] for i in range(len(t))])


@render_ipyvolume
def plot():
    fig = ipv.figure()
    ipv.scatter(
        frames[:, :, 0], frames[:, :, 1], frames[:, :, 2], size=1.5, marker="sphere", color="steelblue"
    )
    return fig


@render_widget
def controls():
    fig = plot.value  # re-runs once `plot` first renders (None before that)
    if fig is None:
        return None
    # add=False returns the Play/slider HBox; the default add=True instead
    # targets the pylab container (invisible here, since `plot` returns the
    # bare Figure) and returns None.
    return ipv.animation_control(fig.scatters[0], add=False, interval=40)

animation

Reacting to interaction

camera() is the one accessor every @render_ipyvolume output has: orbit the figure and read the live position back in Python, the same way you’d call input.my_slider().

import ipyvolume as ipv
import numpy as np
from shiny.express import render, ui

from shinywidgets import render_ipyvolume

ui.page_opts(title="camera() accessor", fillable=True)

ui.markdown("Drag to orbit the scatter; the camera's live position updates below.")


@render_ipyvolume
def plot():
    rng = np.random.default_rng(0)
    x, y, z = rng.normal(size=(3, 300))
    fig = ipv.figure()
    ipv.scatter(x, y, z, size=1, marker="sphere", color="tomato")
    return fig


@render.code
def camera_position():
    # Fires once per completed orbit; the same live pythreejs.Camera instance
    # is returned every time, so read what you need rather than storing it.
    cam = plot.camera()
    return f"position = ({cam.position[0]:.2f}, {cam.position[1]:.2f}, {cam.position[2]:.2f})"

Updating in place

Mutating plot.widget in place – rather than re-rendering – keeps the same WebGL canvas and camera, and re-sends only the trait that changed (not the whole point cloud). Use an @reactive.effect for this; wrap reads of your own reactive state in reactive.isolate() if you’d otherwise create a feedback loop.

import ipyvolume as ipv
import numpy as np
from shiny import reactive
from shiny.express import input, render, ui

from shinywidgets import render_ipyvolume

# Re-rendering would reset the camera (and re-send all 2,000 points). Mutating
# the already-rendered Scatter's traits in an @reactive.effect instead keeps
# the same WebGL scene -- only the changed trait crosses the wire.
ui.page_opts(title="In-place updates", fillable=True)

ui.input_slider("size", "Point size", 0.2, 3, 1, step=0.1)
ui.input_select("color", "Color", ["tomato", "steelblue", "seagreen"])

rng = np.random.default_rng(0)
pts = rng.normal(size=(3, 2000))


@render_ipyvolume
def plot():
    fig = ipv.figure()
    ipv.scatter(*pts, size=1, marker="sphere", color="tomato")
    return fig


@reactive.effect
def _update_size():
    plot.widget.scatters[0].size = input.size()


@reactive.effect
def _update_color():
    # A single CSS color name is a solid color for every point (cheap, and
    # safe -- see the Gotchas section on *per-point* color arrays).
    plot.widget.scatters[0].color = input.color()


@render.code
def camera_unchanged():
    # Orbit the view, then move the sliders: the camera position you set by
    # dragging is untouched by either effect above.
    cam = plot.camera()
    return f"camera position = ({cam.position[0]:.2f}, {cam.position[1]:.2f}, {cam.position[2]:.2f})"

In-place updates beat re-rendering whenever the shape of the data doesn’t change: the browser already has the point cloud, so there’s no reason to re-send it or reset the view. Re-render instead when the dataset itself changes (different x/y/z arrays) or axis limits need to change – direct construction doesn’t auto-fit, so a genuinely new dataset usually needs new xlim/ylim/zlim too.

Linking widgets

Camera-linked twin views

camera() driving another view: orbit one figure, and a second “chase” figure copies the same camera pose. This is the pattern behind an overview/detail pair, or two different representations of the same scene kept in visual sync.

import ipyvolume as ipv
import numpy as np
from shiny import reactive
from shiny.express import ui

from shinywidgets import render_ipyvolume

# camera() driving another view: orbit the big plot, and a small "chase" view
# mirrors the same camera pose -- a common pattern for a detail/overview pair.
ui.page_opts(title="Linked camera views", fillable=True)

rng = np.random.default_rng(0)
pts = rng.normal(size=(3, 1500))

with ui.layout_columns(col_widths=(8, 4)):
    with ui.card():
        ui.card_header("Orbit this one")

        @render_ipyvolume
        def main_plot():
            fig = ipv.figure()
            ipv.scatter(*pts, size=0.8, marker="sphere", color="tomato")
            return fig

    with ui.card():
        ui.card_header("Chase view (mirrors the camera)")

        @render_ipyvolume
        def chase_plot():
            fig = ipv.figure()
            ipv.scatter(*pts, size=0.8, marker="sphere", color="steelblue")
            return fig


@reactive.effect
def _sync_chase_camera():
    cam = main_plot.camera()
    chase_cam = chase_plot.widget.camera
    chase_cam.position = cam.position
    chase_cam.quaternion = cam.quaternion
    chase_cam.zoom = cam.zoom

Orbiting the left figure moves the right one to match, in the same reactive cycle:

Table-linked highlight

ipyvolume can’t tell you which points a lasso covers, but nothing stops driving a highlight the other way: select a row in an ordinary data table and highlight the matching point in 3D. The example recomputes plain color/size arrays rather than using ipyvolume’s native selected mechanism, which has known bugs (see Gotchas).

import ipyvolume as ipv
import numpy as np
import plotly.express as px
from shiny.express import render, ui

from shinywidgets import render_ipyvolume

# Table row selection -> 3D highlight: ipyvolume can't tell you which points a
# browser lasso covers (see Gotchas), but a table can drive a highlight the
# other way.
#
# This bakes the highlight into plain per-point `color`/`size` arrays rather
# than ipyvolume's dedicated `selected`/`size_selected`/`color_selected`
# mechanism. `selected` has two upstream bugs (see Gotchas): an empty
# `selected=[]` crashes the view, and even with a non-empty sentinel standing
# in for "nothing selected", the highlight only ever reflects the *first*
# value `selected` is set to after a figure renders; clicking a different row
# afterwards changes the trait (visible via `reactive_read`) but never moves
# the highlighted sphere, with no console error. Recomputing a plain
# color/size array on every selection sidesteps both bugs and re-renders the
# whole figure (the camera resets), which is a reasonable trade for a
# table-driven highlight.
ui.page_opts(title="Table row selection -> 3D highlight", fillable=True)

iris = px.data.iris()
palette = {1: (0.91, 0.30, 0.24), 2: (0.18, 0.49, 0.20), 3: (0.21, 0.45, 0.69)}
base_color = np.array([palette[i] for i in iris["species_id"]])
HIGHLIGHT = np.array([1.0, 1.0, 0.0])  # yellow

with ui.layout_columns():
    with ui.card():
        ui.card_header("Click a row to highlight it in 3D")

        @render.data_frame
        def table():
            return render.DataGrid(
                iris[["sepal_length", "petal_length", "sepal_width", "species"]],
                selection_mode="row",
                height="400px",
            )

    with ui.card():
        ui.card_header("Highlighted point")

        @render_ipyvolume
        def plot():
            rows = list(table.cell_selection()["rows"])
            color = base_color.copy()
            size = np.full(len(iris), 1.2)
            if rows:
                color[rows] = HIGHLIGHT
                size[rows] = 6
            fig = ipv.figure()
            ipv.scatter(
                iris["sepal_length"], iris["petal_length"], iris["sepal_width"],
                size=size, color=color, marker="sphere",
            )
            return fig

Clicking a second, different row relocates the highlight correctly – unlike ipyvolume’s native selected mechanism, which gets stuck on the first point it ever highlights:

table moved

Because this recomputes the color/size arrays on every selection, the whole figure re-renders (the camera resets) – a reasonable trade for a table-driven highlight.

Sequence-linked chart

The Play control’s current frame drives a vertical marker on a 2D speed chart that tracks the comet as it animates along a Lorenz attractor:

import ipyvolume as ipv
import numpy as np
from shiny.express import render, ui

from shinywidgets import reactive_read, render_ipyvolume, render_widget

# Animated scatter driving a 2D companion chart: `sequence_index` isn't an
# accessor (ipyvolume has none for it), but it's an ordinary synced trait, so
# `reactive_read()` turns the Play button's current frame into a reactive
# value any other output can use -- here, a vertical marker on a speed chart.
ui.page_opts(title="Sequence index -> companion chart", fillable=True)


def lorenz(n=4000, dt=0.006, sigma=10.0, rho=28.0, beta=8 / 3):
    pts = np.empty((n, 3))
    p = np.array([1.0, 1.0, 1.0])
    for i in range(n):
        v = np.array([sigma * (p[1] - p[0]), p[0] * (rho - p[2]) - p[1], p[0] * p[1] - beta * p[2]])
        p += dt * v
        pts[i] = p
    speed = np.linalg.norm(np.gradient(pts, axis=0), axis=1)
    return pts, speed


pts, speed = lorenz()
xyz = (pts - pts.mean(axis=0)) / pts.std(axis=0)

S, TAIL = 120, 50
starts = np.linspace(0, len(xyz) - TAIL - 1, S).astype(int)
comet = np.stack([xyz[s : s + TAIL] for s in starts], axis=0).astype(np.float32)

with ui.layout_columns():
    with ui.card():
        ui.card_header("Comet along the Lorenz attractor")

        @render_ipyvolume
        def plot():
            fig = ipv.figure()
            ipv.scatter(comet[:, :, 0], comet[:, :, 1], comet[:, :, 2], size=1, marker="sphere", color="tomato")
            return fig

        @render_widget
        def controls():
            fig = plot.value
            if fig is None:
                return None
            return ipv.animation_control(fig.scatters[0], add=False, interval=50)

    with ui.card():
        ui.card_header("Speed along the trajectory (marker = current frame)")

        @render.plot
        def speed_chart():
            import matplotlib.pyplot as plt

            frame = reactive_read(plot.widget.scatters[0], "sequence_index")
            fig, ax = plt.subplots()
            ax.plot(speed, color="steelblue", linewidth=0.8)
            ax.axvline(starts[frame], color="tomato", linewidth=2)
            ax.set_xlabel("step")
            ax.set_ylabel("speed")
            return fig

Accessor reference

Accessor Returns Before any interaction Across re-render Default rate policy
camera() the live pythreejs.Camera (same mutable instance every read) seeded value: the figure’s camera as constructed (never cancels unless fig.camera is explicitly None) re-seeded to the new figure’s camera none – the browser already coalesces a whole orbit gesture into one update

camera() fires when the camera’s position/quaternion/zoom change (end of an orbit/zoom) or the camera is replaced.

That’s the only accessor ipyvolume has. There is no selected()/clicked() accessor: selections arrive as raw pixel-region custom messages, not indices, so there’s nothing index-shaped to expose.

camera()

camera() doesn’t wait for an orbit to return something – read it with no prior interaction and it reports the figure’s camera as constructed. It only cancels (like req(False)) in the narrow case where fig.camera is explicitly set to None, which the pylab API never does on its own. Across a re-render it re-seeds to the new figure’s camera rather than keeping the old orbit, since it describes the camera that exists right now. (plot.read("camera") is not a substitute for plot.camera(): it returns the raw synced snapshot, not the live Camera object – use the named method.)

Tuning accessors

Beyond the accessor

A couple of other synced traits are worth reading directly, since they’re not accessors:

  • reactive_read(plot.widget, "width") / "height" – the figure’s canvas size in pixels. In a filling layout, the browser measures and resizes the canvas, then syncs the measured size back to these traits; reading them tells you the actual rendered size.
  • reactive_read(scatter, "sequence_index") – the current animation frame, which the Play control advances; used to drive the companion chart in Sequence-linked chart above.
import ipyvolume as ipv
import numpy as np
from shiny.express import render, ui

from shinywidgets import reactive_read, render_ipyvolume, render_widget

ui.page_opts(title="Beyond the accessors", fillable=True)

t = np.linspace(0, 4 * np.pi, 300)
helix = np.stack([np.cos(t), np.sin(t), t / 4], axis=1)
frames = np.stack([np.roll(helix, -i, axis=0)[:40] for i in range(len(t))])

with ui.card(height="320px"):

    @render_ipyvolume
    def plot():
        fig = ipv.figure()
        ipv.scatter(
            frames[:, :, 0], frames[:, :, 1], frames[:, :, 2], size=1.5, marker="sphere", color="steelblue"
        )
        return fig


@render_widget
def controls():
    fig = plot.value
    if fig is None:
        return None
    return ipv.animation_control(fig.scatters[0], add=False, interval=40)


@render.code
def state():
    # The browser syncs the figure's resized canvas back to these traits, and
    # the Play control advances this trait as it animates -- neither is an
    # accessor (no dedicated method), but both are ordinary synced traits.
    w = reactive_read(plot.widget, "width")
    h = reactive_read(plot.widget, "height")
    frame = reactive_read(plot.widget.scatters[0], "sequence_index")
    return f"canvas = {w}x{h}px\nframe = {frame}"

reactive_read

A trait not worth reading this way: scatter.selected. It’s a real synced trait, but it only flows Python-to-browser – reactive_read() on it will only ever show you what you last set from Python, never what the user lassoed.

Rate limiting

camera() has no default rate policy – an orbit already produces one update per gesture, so there’s nothing to coalesce. rate_policy/rate_delay still work on render_ipyvolume, but for this widget they only affect plain reactive_read() calls on the widget, not camera() itself.

This matters for a trait that can update repeatedly, like the canvas size during a container resize. The example below renders two identical figures side by side and counts how often a reactive_read()-driven text output recomputes during a burst of resizes – one unthrottled, one debounced:

import ipyvolume as ipv
import numpy as np
from shiny import reactive
from shiny.express import render, ui

from shinywidgets import reactive_read, render_ipyvolume

# camera() has no default rate policy: a drag already collapses to one trait
# update at the end of the orbit, so there's nothing bursty to coalesce.
# rate_policy/rate_delay still apply to plain reactive_read() calls on the
# widget -- useful for a trait that *does* update repeatedly, like the canvas
# size while its container is being resized.
ui.page_opts(title="Rate limiting", fillable=True)

rng = np.random.default_rng(0)
pts = rng.normal(size=(3, 300))


def make_fig():
    fig = ipv.figure()
    ipv.scatter(*pts, size=1, marker="sphere", color="tomato")
    return fig


with ui.layout_columns():
    with ui.card():
        ui.card_header("Unthrottled")

        @render_ipyvolume
        def plot_live():
            return make_fig()

    with ui.card():
        ui.card_header("Debounced 500ms")

        @render_ipyvolume(rate_policy="debounce", rate_delay=0.5)
        def plot_debounced():
            return make_fig()


live_count = reactive.value(0)
debounced_count = reactive.value(0)


@render.code
def live_recomputes():
    reactive_read(plot_live.widget, "width")
    with reactive.isolate():
        n = live_count.get() + 1
        live_count.set(n)
    return f"unthrottled recomputes: {n}"


@render.code
def debounced_recomputes():
    reactive_read(plot_debounced.widget, "width")
    with reactive.isolate():
        n = debounced_count.get() + 1
        debounced_count.set(n)
    return f"debounced recomputes: {n}"

Mid-burst, the unthrottled counter has already advanced with every resize; the debounced one hasn’t moved yet:

rate limiting mid-burst

Large data

ipyvolume draws with the GPU (WebGL), so it’s the “draw it with the GPU in the browser” approach – see Large data & performance’s three approaches. Scatter/quiver markers are GPU-instanced and comfortable into the tens of thousands of points; volume rendering (ipyvolume.volshow) ray-marches a full 3D array in the browser, so its cost is dominated by texture upload size, not point count. For single-figure point clouds past 100k points, or voxel grids larger than a texture upload wants to carry, reach for k3d instead – it’s built for exactly that scale.

Layout and sizing

A figure’s WebGL canvas is resized to fill its container (and tracks later resizes) unless the figure has an explicit width/height – set via ipv.figure(width=..., height=...), Layout(height=...), or output_widget(height=...). An explicitly sized figure keeps its pixel size even inside a differently sized output (the output clips or pads it, rather than the output’s CSS stretching the canvas).

import ipyvolume as ipv
import numpy as np
from shiny.express import ui

from shinywidgets import render_ipyvolume

ui.page_opts(title="Layout and sizing", fillable=True)

rng = np.random.default_rng(0)
pts = rng.normal(size=(3, 500))


def make_fig(**kwargs):
    fig = ipv.figure(**kwargs)
    ipv.scatter(*pts, size=1, marker="sphere", color="tomato")
    return fig


with ui.layout_columns():
    with ui.card(full_screen=True):
        ui.card_header("Fills its card (try full_screen)")

        @render_ipyvolume
        def fill_plot():
            return make_fig()

    with ui.card():
        ui.card_header("Explicit figure size (400x300px): clipped, not stretched")

        @render_ipyvolume
        def fixed_plot():
            return make_fig(width=400, height=300)

layout

full_screen=True on the card works as expected (the canvas grows to fill the expanded card and shrinks back on exit).

Gotchas

  • A per-point array of color strings (color=["tomato", "seagreen", ...]) hangs the whole server process, not just that output – even an unrelated request to a different session stops responding. Use a solid color string, or a numeric (N, 3)/(N, 4) RGB(A) float array, for per-point coloring instead.
  • Direct construction (ipyvolume.Figure() with Scatter/Mesh objects assigned to fig.scatters) doesn’t auto-fit axes, defaulting to a [0, 1]^3 box. Set fig.xlim/ylim/zlim yourself, or use the pylab drawing functions (ipv.scatter(), ipv.quiver(), ipv.plot_surface(), …), which fit them for you.
  • A bare ipyvolume.widgets.Scatter(...) needs size_selected/color_selected set explicitly, since their defaults don’t JSON-serialize. The pylab functions set sensible defaults for you.
  • The native selected trait has two upstream bugs: setting it to an empty list crashes the view, and it only ever reflects the first value set after a render, never a later one. Recompute plain color/size arrays instead, as in Table-linked highlight above.

Learn more