Skip to content

Interactivity

In a notebook, and in HTML export, ggrust plots are interactive. The picture is drawn by Rust; a small embedded script (no download, no CDN) adds the interaction.

Tooltips

Hovering a mark shows a tooltip with the values mapped to it. layer_tooltips() chooses the lines: @column inserts a value, text before | is the line's label. tooltips="none" turns them off for a layer.

import polars as pl
from ggrust import *

DATA = "https://raw.githubusercontent.com/datavil/ggrust/main/docs/data/"
homes = pl.read_csv(DATA + "homes.csv")

tooltips = (
    layer_tooltips()
    .title("@neighbourhood")
    .line("Price|@{price_k}k")
    .line("Area|@{area_m2} m²")
    .line("Rooms|@rooms")
    .line("Built|@year_built")
    .format("year_built", "d")
)
plot = (
    ggplot(homes, aes("area_m2", "price_k", color="neighbourhood"))
    + geom_point(size=3, alpha=0.7, tooltips=tooltips)
    + labs(x="Floor area (m²)", y="Price (thousands)", color="")
    + theme_minimal()
    + ggtb()
    + ggsize(640, 420)
)
plot

.format("price_k", ".0f") formats a value, .anchor("top_right") pins the tooltip to a corner and .min_width(...) sets its width.

Zoom and pan

Add ggtb() for the toolbar above the plot (try it on the plot above). It has pan, rubber-band zoom, centerpoint zoom and reset. Axes are recomputed at every step, so breaks and labels stay readable.

Large data

Point layers with more than 50,000 points are drawn on a canvas from binary buffers instead of thousands of SVG elements, so zooming stays fast. ggrust does not sample: every row is drawn (lets-plot samples layers above 100,000 points by default).

import numpy as np
import polars as pl
from ggrust import *

generator = np.random.default_rng(7)
n = 100_000
angle = generator.uniform(0, 2 * np.pi, n)
radius = generator.normal(1.0, 0.15, n) + 0.3 * np.sin(5 * angle)
frame = pl.DataFrame(
    {
        "x": radius * np.cos(angle),
        "y": radius * np.sin(angle),
        "angle": angle,
    }
)

plot = (
    ggplot(frame, aes("x", "y", color="angle"))
    + geom_point(size=0.5, alpha=0.3, tooltips="none")
    + scale_color_gradientn(colors=["#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"])
    + coord_fixed()
    + theme_void()
    + theme(legend_position="none")
    + ggtb()
    + ggsize(500, 500)
)
plot