Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Fix mo.ui.matplotlib pixel bounds calcuation (#8419)

Akshay Agrawaldfbe9f40b98c
diff --git a/frontend/src/plugins/impl/matplotlib/__tests__/matplotlib-renderer.test.ts b/frontend/src/plugins/impl/matplotlib/__tests__/matplotlib-renderer.test.ts
index c68443b592..4213f898dc 100644
--- a/frontend/src/plugins/impl/matplotlib/__tests__/matplotlib-renderer.test.ts
+++ b/frontend/src/plugins/impl/matplotlib/__tests__/matplotlib-renderer.test.ts
@@ -3,8 +3,14 @@ import { describe, expect, it } from "vitest";
import type { Data } from "../matplotlib-renderer";
import { visibleForTesting } from "../matplotlib-renderer";
-const { pixelToData, dataToPixel, pointInPolygon, clampToAxes, isPointInBox } =
- visibleForTesting;
+const {
+ pixelToData,
+ dataToPixel,
+ pointInPolygon,
+ clampToAxes,
+ isPointInBox,
+ isInAxes,
+} = visibleForTesting;
// A simple axes geometry for testing:
// axes occupy pixels [100, 50] to [500, 350] (400px wide, 300px tall)
@@ -150,3 +156,32 @@ describe("isPointInBox", () => {
expect(isPointInBox({ x: 5, y: 30 }, boxEnd, boxStart)).toBe(false);
});
});
+
+describe("isInAxes", () => {
+ it("returns true for a point inside the axes", () => {
+ expect(isInAxes({ x: 300, y: 200 }, LINEAR_AXES)).toBe(true);
+ });
+
+ it("returns true for a point on the boundary", () => {
+ expect(isInAxes({ x: 100, y: 50 }, LINEAR_AXES)).toBe(true); // top-left
+ expect(isInAxes({ x: 500, y: 350 }, LINEAR_AXES)).toBe(true); // bottom-right
+ expect(isInAxes({ x: 100, y: 350 }, LINEAR_AXES)).toBe(true); // bottom-left
+ expect(isInAxes({ x: 500, y: 50 }, LINEAR_AXES)).toBe(true); // top-right
+ });
+
+ it("returns false for a point left of axes", () => {
+ expect(isInAxes({ x: 99, y: 200 }, LINEAR_AXES)).toBe(false);
+ });
+
+ it("returns false for a point right of axes", () => {
+ expect(isInAxes({ x: 501, y: 200 }, LINEAR_AXES)).toBe(false);
+ });
+
+ it("returns false for a point above axes", () => {
+ expect(isInAxes({ x: 300, y: 49 }, LINEAR_AXES)).toBe(false);
+ });
+
+ it("returns false for a point below axes", () => {
+ expect(isInAxes({ x: 300, y: 351 }, LINEAR_AXES)).toBe(false);
+ });
+});
diff --git a/frontend/src/plugins/impl/matplotlib/matplotlib-renderer.ts b/frontend/src/plugins/impl/matplotlib/matplotlib-renderer.ts
index b7fca724f2..cdedd67a1f 100644
--- a/frontend/src/plugins/impl/matplotlib/matplotlib-renderer.ts
+++ b/frontend/src/plugins/impl/matplotlib/matplotlib-renderer.ts
@@ -257,6 +257,11 @@ function isPointInBox(
return pt.x >= minX && pt.x <= maxX && pt.y >= minY && pt.y <= maxY;
}
+function isInAxes(pt: PixelPoint, g: AxesGeometry): boolean {
+ const [axLeft, axTop, axRight, axBottom] = g.axesPixelBounds;
+ return pt.x >= axLeft && pt.x <= axRight && pt.y >= axTop && pt.y <= axBottom;
+}
+
export const visibleForTesting = {
createScale,
pixelToData,
@@ -264,6 +269,7 @@ export const visibleForTesting = {
pointInPolygon,
clampToAxes,
isPointInBox,
+ isInAxes,
};
export class MatplotlibRenderer {
@@ -568,6 +574,9 @@ export class MatplotlibRenderer {
// Shift+click -> start lasso
if (e.shiftKey) {
+ if (!isInAxes(pt, this.#state)) {
+ return;
+ }
this.#interaction = {
type: "lasso",
points: [clampToAxes(pt, this.#state)],
@@ -620,7 +629,10 @@ export class MatplotlibRenderer {
this.#clearSelection();
}
- // Start new box selection
+ // Start new box selection (only inside axes)
+ if (!isInAxes(pt, this.#state)) {
+ return;
+ }
const clamped = clampToAxes(pt, this.#state);
this.#interaction = {
type: "box",
diff --git a/marimo/_plugins/ui/_impl/mpl.py b/marimo/_plugins/ui/_impl/mpl.py
index d4ad978404..8a9d7fa2e7 100644
--- a/marimo/_plugins/ui/_impl/mpl.py
+++ b/marimo/_plugins/ui/_impl/mpl.py
@@ -135,7 +135,7 @@ def _figure_pixel_size(figure: Figure) -> tuple[float, float]:
def _figure_to_base64(figure: Figure) -> str:
"""Render a matplotlib figure to a base64-encoded PNG data URL."""
buf = io.BytesIO()
- figure.savefig(buf, format="png")
+ figure.savefig(buf, format="png", dpi=figure.get_dpi(), bbox_inches=None)
buf.seek(0)
encoded = base64.b64encode(buf.read()).decode("utf-8")
buf.close()
@@ -204,18 +204,7 @@ class matplotlib(UIElement[dict[str, JSONType], MatplotlibSelection]):
raise ValueError("Axes must be attached to a figure.")
self._ax: Axes = axes
- fig_width_px, fig_height_px = _figure_pixel_size(figure)
-
- # Axes pixel bounds: [left, top, right, bottom]
- # relative to the full figure image
- bbox = axes.get_position()
- axes_pixel_bounds: list[float] = [
- bbox.x0 * fig_width_px, # left
- (1 - bbox.y1) * fig_height_px, # top
- bbox.x1 * fig_width_px, # right
- (1 - bbox.y0) * fig_height_px, # bottom
- ]
-
+ # Validate scales first (fail fast, no side effects)
_SUPPORTED_SCALES = ("linear", "log")
x_scale = axes.get_xscale()
y_scale = axes.get_yscale()
@@ -230,12 +219,26 @@ class matplotlib(UIElement[dict[str, JSONType], MatplotlibSelection]):
f"mo.ui.matplotlib supports: {', '.join(_SUPPORTED_SCALES)}."
)
+ # Render the figure first — savefig triggers the draw which
+ # finalizes layout (tight_layout, constrained_layout, etc.)
+ chart_base64 = _figure_to_base64(figure)
+
+ # Now capture axes position — reflects post-layout bounds
+ fig_width_px, fig_height_px = _figure_pixel_size(figure)
+ bbox = axes.get_position()
+ axes_pixel_bounds: list[float] = [
+ bbox.x0 * fig_width_px, # left
+ (1 - bbox.y1) * fig_height_px, # top
+ bbox.x1 * fig_width_px, # right
+ (1 - bbox.y0) * fig_height_px, # bottom
+ ]
+
super().__init__(
component_name=matplotlib.name,
initial_value={},
label="",
args={
- "chart-base64": _figure_to_base64(figure),
+ "chart-base64": chart_base64,
"x-bounds": list(axes.get_xlim()),
"y-bounds": list(axes.get_ylim()),
"axes-pixel-bounds": axes_pixel_bounds,
diff --git a/marimo/_smoke_tests/ui_matplotlib.py b/marimo/_smoke_tests/ui_matplotlib.py
index 5dfd744eea..93d3924f63 100644
--- a/marimo/_smoke_tests/ui_matplotlib.py
+++ b/marimo/_smoke_tests/ui_matplotlib.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.19.11"
+__generated_with = "0.20.1"
app = marimo.App()
@@ -39,16 +39,12 @@ def _(compute_embedding):
@app.cell
-def _(embedding, mnist, plt):
+def _(embedding, mnist, plt, pymde):
x = embedding[:, 0]
y = embedding[:, 1]
- plt.scatter(x=x, y=y, s=0.05, cmap="Spectral", c=mnist.attributes["digits"])
- plt.yticks([-2, 0, 2])
- plt.xticks([-2, 0, 2])
- plt.ylim(-2.5, 2.5)
- plt.xlim(-2.5, 2.5)
- ax = plt.gca()
+ ax = pymde.plot(X=embedding, color_by=mnist.attributes["digits"])
+ plt.tight_layout()
return ax, x, y
@@ -71,6 +67,71 @@ def _(fig):
return
[email protected](hide_code=True)
+def _(mo):
+ mo.md("""
+ ## Edge-data test
+
+ Points are clustered right at the axes edges. Clicking on tick labels,
+ axis labels, or the title should **not** start a selection. Previously
+ the click was clamped to the nearest edge, which would select these
+ edge points via `get_mask()`.
+ """)
+ return
+
+
+def _(np, plt):
+ rng = np.random.default_rng(99)
+ # Points hugging the four edges of [0, 10] x [0, 10]
+ edge_n = 30
+ edge_x = np.concatenate([
+ rng.uniform(0, 0.3, edge_n), # left edge
+ rng.uniform(9.7, 10, edge_n), # right edge
+ rng.uniform(0, 10, edge_n), # bottom edge
+ rng.uniform(0, 10, edge_n), # top edge
+ rng.uniform(3, 7, edge_n), # centre (control)
+ ])
+ edge_y = np.concatenate([
+ rng.uniform(0, 10, edge_n), # left edge
+ rng.uniform(0, 10, edge_n), # right edge
+ rng.uniform(0, 0.3, edge_n), # bottom edge
+ rng.uniform(9.7, 10, edge_n), # top edge
+ rng.uniform(3, 7, edge_n), # centre (control)
+ ])
+
+ plt.figure()
+ plt.scatter(edge_x, edge_y, s=20, c=edge_y, cmap="viridis")
+ plt.colorbar(label="y value")
+ plt.xlim(0, 10)
+ plt.ylim(0, 10)
+ plt.xlabel("X axis (click here should NOT select)")
+ plt.ylabel("Y axis (click here should NOT select)")
+ plt.title("Title area (click here should NOT select)")
+ edge_ax = plt.gca()
+ return edge_ax, edge_x, edge_y
+
+
+def _(edge_ax, mo):
+ edge_fig = mo.ui.matplotlib(edge_ax)
+ edge_fig
+ return (edge_fig,)
+
+
+def _(edge_fig, edge_x, edge_y):
+ _m = edge_fig.value.get_mask(edge_x, edge_y)
+ f"Selected {_m.sum()} / {len(edge_x)} points"
+ return
+
+
+def _(edge_fig):
+ edge_fig.value if edge_fig.value else "No selection!"
+ return
+
+
@app.cell(hide_code=True)
def _(mo):
mo.md("""
diff --git a/tests/_plugins/ui/_impl/test_ui_mpl.py b/tests/_plugins/ui/_impl/test_ui_mpl.py
index 08a0e99d45..e375a8621e 100644
--- a/tests/_plugins/ui/_impl/test_ui_mpl.py
+++ b/tests/_plugins/ui/_impl/test_ui_mpl.py
@@ -1,6 +1,7 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+import io
from typing import Any
import matplotlib.pyplot as plt # noqa: E402
@@ -260,6 +261,72 @@ def test_figure_to_base64() -> None:
# ============================================================================
+def test_axes_bounds_match_constrained_layout() -> None:
+ """Axes bounds should reflect post-layout position with constrained_layout."""
+ fig, ax = plt.subplots(constrained_layout=True)
+ ax.scatter([1, 2, 3], [4, 5, 6])
+ ax.set_xlabel("X label")
+ ax.set_ylabel("Y label")
+ ax.set_title("Title")
+ plt.close(fig)
+
+ widget = matplotlib(ax)
+ args = widget._component_args
+
+ # Render independently with the same settings to get expected bounds
+ fig2 = ax.get_figure()
+ fig2.savefig(
+ io.BytesIO(), format="png", dpi=fig2.get_dpi(), bbox_inches=None
+ )
+ bbox = ax.get_position()
+ w, h = _figure_pixel_size(fig2)
+
+ expected = [
+ bbox.x0 * w,
+ (1 - bbox.y1) * h,
+ bbox.x1 * w,
+ (1 - bbox.y0) * h,
+ ]
+ for actual, exp in zip(args["axes-pixel-bounds"], expected):
+ assert abs(actual - exp) < 1e-6
+
+
+def test_axes_bounds_match_tight_layout() -> None:
+ """Axes bounds should reflect post-layout position with tight_layout."""
+ fig, ax = plt.subplots()
+ ax.scatter([1, 2, 3], [4, 5, 6])
+ ax.set_xlabel("X label")
+ ax.set_ylabel("Y label")
+ ax.set_title("Title")
+ fig.tight_layout()
+ plt.close(fig)
+
+ widget = matplotlib(ax)
+ args = widget._component_args
+
+ # Render independently with the same settings to get expected bounds
+ fig2 = ax.get_figure()
+ fig2.savefig(
+ io.BytesIO(), format="png", dpi=fig2.get_dpi(), bbox_inches=None
+ )
+ bbox = ax.get_position()
+ w, h = _figure_pixel_size(fig2)
+
+ expected = [
+ bbox.x0 * w,
+ (1 - bbox.y1) * h,
+ bbox.x1 * w,
+ (1 - bbox.y0) * h,
+ ]
+ for actual, exp in zip(args["axes-pixel-bounds"], expected):
+ assert abs(actual - exp) < 1e-6
+
+
+# ============================================================================
+# HTML output tests
+# ============================================================================
+
+
def test_html_contains_tag() -> None:
ax = _make_scatter_ax()
fig = matplotlib(ax)