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: reset Plotly axis settings when switching between different chart types (#9067)

Myles Scolnick986886f5294d
diff --git a/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts b/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts
index 135e3d37cf..0a68d21552 100644
--- a/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts
+++ b/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts
@@ -8,11 +8,15 @@ import {
computeLayoutUpdate,
computeOmitKeys,
createInitialLayout,
+ hasCompatibleTraces,
} from "../usePlotlyLayout";
-function createFigure(layoutOverrides: Partial<Plotly.Layout> = {}): Figure {
+function createFigure(
+ layoutOverrides: Partial<Plotly.Layout> = {},
+ data: Plotly.Data[] = [],
+): Figure {
return {
- data: [],
+ data,
layout: { ...layoutOverrides } as Plotly.Layout,
frames: null,
};
@@ -35,9 +39,46 @@ describe("createInitialLayout", () => {
});
});
+describe("hasCompatibleTraces", () => {
+ it("returns true for same trace types", () => {
+ const a = createFigure({}, [{ type: "scatter" } as Plotly.Data]);
+ const b = createFigure({}, [{ type: "scatter" } as Plotly.Data]);
+ expect(hasCompatibleTraces(a, b)).toBe(true);
+ });
+
+ it("returns true for default scatter types (undefined type)", () => {
+ const a = createFigure({}, [{} as Plotly.Data]);
+ const b = createFigure({}, [{ type: "scatter" } as Plotly.Data]);
+ expect(hasCompatibleTraces(a, b)).toBe(true);
+ });
+
+ it("returns false for different trace types", () => {
+ const a = createFigure({}, [{ type: "scatter" } as Plotly.Data]);
+ const b = createFigure({}, [{ type: "histogram" } as Plotly.Data]);
+ expect(hasCompatibleTraces(a, b)).toBe(false);
+ });
+
+ it("returns false for different number of traces", () => {
+ const a = createFigure({}, [{ type: "scatter" } as Plotly.Data]);
+ const b = createFigure({}, [
+ { type: "scatter" } as Plotly.Data,
+ { type: "scatter" } as Plotly.Data,
+ ]);
+ expect(hasCompatibleTraces(a, b)).toBe(false);
+ });
+
+ it("returns true for empty data arrays", () => {
+ const a = createFigure({}, []);
+ const b = createFigure({}, []);
+ expect(hasCompatibleTraces(a, b)).toBe(true);
+ });
+});
+
describe("computeLayoutOnFigureChange", () => {
- it("preserves only dragmode/xaxis/yaxis from previous layout (#7964)", () => {
- const nextFigure = createFigure({ title: { text: "New" } });
+ it("preserves only dragmode/xaxis/yaxis from previous layout for compatible traces (#7964)", () => {
+ const scatterData = [{ type: "scatter" } as Plotly.Data];
+ const prevFigure = createFigure({}, scatterData);
+ const nextFigure = createFigure({ title: { text: "New" } }, scatterData);
const prevLayout: Partial<Plotly.Layout> = {
dragmode: "zoom",
xaxis: { range: [0, 10] },
@@ -46,7 +87,11 @@ describe("computeLayoutOnFigureChange", () => {
annotations: [{ text: "Old", x: 0, y: 0 }],
};
- const result = computeLayoutOnFigureChange(nextFigure, prevLayout);
+ const result = computeLayoutOnFigureChange(
+ nextFigure,
+ prevFigure,
+ prevLayout,
+ );
// Preserved from prev
expect(result.dragmode).toBe("zoom");
@@ -59,15 +104,68 @@ describe("computeLayoutOnFigureChange", () => {
expect(result.annotations).toBeUndefined();
});
+ it("resets axis settings when trace types change (#5898)", () => {
+ const prevFigure = createFigure({}, [{ type: "histogram" } as Plotly.Data]);
+ const nextFigure = createFigure({ title: { text: "Bar" } }, [
+ { type: "bar" } as Plotly.Data,
+ ]);
+ const prevLayout: Partial<Plotly.Layout> = {
+ dragmode: "zoom",
+ xaxis: { range: [-3, 3] },
+ yaxis: { range: [0, 200] },
+ };
+
+ const result = computeLayoutOnFigureChange(
+ nextFigure,
+ prevFigure,
+ prevLayout,
+ );
+
+ // Dragmode is still preserved
+ expect(result.dragmode).toBe("zoom");
+ // Axis settings are NOT preserved — they come from the new figure's layout
+ expect(result.xaxis).toBeUndefined();
+ expect(result.yaxis).toBeUndefined();
+ // New figure's layout is applied
+ expect(result.title).toEqual({ text: "Bar" });
+ });
+
+ it("preserves nextFigure dragmode when prevLayout has no dragmode", () => {
+ const prevFigure = createFigure({}, [{ type: "histogram" } as Plotly.Data]);
+ const nextFigure = createFigure({ dragmode: "lasso" }, [
+ { type: "bar" } as Plotly.Data,
+ ]);
+ const prevLayout: Partial<Plotly.Layout> = {
+ xaxis: { range: [0, 10] },
+ };
+
+ const result = computeLayoutOnFigureChange(
+ nextFigure,
+ prevFigure,
+ prevLayout,
+ );
+
+ // nextFigure.layout.dragmode should be preserved via base, not overwritten
+ expect(result.dragmode).toBe("lasso");
+ // Axis settings are NOT preserved for incompatible traces
+ expect(result.xaxis).toBeUndefined();
+ expect(result.yaxis).toBeUndefined();
+ });
+
it("uses shapes from new figure, not previous layout", () => {
const nextFigure = createFigure({
shapes: [{ type: "circle", x0: 0, x1: 1, y0: 0, y1: 1 }],
});
+ const prevFigure = createFigure({});
const prevLayout: Partial<Plotly.Layout> = {
shapes: [{ type: "rect", x0: 0, x1: 1, y0: 0, y1: 1 }],
};
- const result = computeLayoutOnFigureChange(nextFigure, prevLayout);
+ const result = computeLayoutOnFigureChange(
+ nextFigure,
+ prevFigure,
+ prevLayout,
+ );
expect(result.shapes).toHaveLength(1);
expect(result.shapes?.[0].type).toBe("circle");
diff --git a/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts b/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts
index da3c968d93..481fb4044a 100644
--- a/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts
+++ b/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts
@@ -3,7 +3,7 @@
import { usePrevious } from "@uidotdev/usehooks";
import { dequal as isEqual } from "dequal";
import type * as Plotly from "plotly.js";
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { Objects } from "@/utils/objects";
import type { Figure } from "./Plot";
@@ -39,18 +39,46 @@ export function createInitialLayout(figure: Figure): Partial<Plotly.Layout> {
};
}
+/**
+ * Returns true if two figures have compatible trace types.
+ * When traces are incompatible (different types, count, or order), axis settings
+ * from the old figure should not be preserved as they would distort the
+ * new chart. See https://github.com/marimo-team/marimo/issues/5898
+ */
+export function hasCompatibleTraces(prev: Figure, next: Figure): boolean {
+ if (prev.data.length !== next.data.length) {
+ return false;
+ }
+ return prev.data.every(
+ (trace, i) =>
+ (trace.type ?? "scatter") === (next.data[i]?.type ?? "scatter"),
+ );
+}
+
/**
* Computes the updated layout when the figure changes.
* Preserves user-interaction values (dragmode, xaxis, yaxis) while
* taking everything else from the new figure's layout.
+ *
+ * When trace types change, only dragmode is preserved — axis settings
+ * are reset to let Plotly auto-compute ranges for the new chart type.
*/
export function computeLayoutOnFigureChange(
nextFigure: Figure,
+ prevFigure: Figure,
prevLayout: Partial<Plotly.Layout>,
): Partial<Plotly.Layout> {
+ const base = createInitialLayout(nextFigure);
+ if (hasCompatibleTraces(prevFigure, nextFigure)) {
+ return {
+ ...base,
+ ...Objects.pick(prevLayout, PERSISTED_LAYOUT_KEYS),
+ };
+ }
+ // Incompatible traces — only preserve dragmode, not axis settings
return {
- ...createInitialLayout(nextFigure),
- ...Objects.pick(prevLayout, PERSISTED_LAYOUT_KEYS),
+ ...base,
+ ...("dragmode" in prevLayout ? { dragmode: prevLayout.dragmode } : {}),
};
}
@@ -121,6 +149,9 @@ export function usePlotlyLayout({
return structuredClone(originalFigure);
});
+ // Track the previous figure to detect trace type changes
+ const prevFigureRef = useRef(figure);
+
const [layout, setLayout] = useState<Partial<Plotly.Layout>>(() => {
return {
...createInitialLayout(figure),
@@ -132,12 +163,15 @@ export function usePlotlyLayout({
// Update figure and layout when originalFigure changes
useEffect(() => {
const nextFigure = structuredClone(originalFigure);
+ const prevFig = prevFigureRef.current;
+ prevFigureRef.current = nextFigure;
setFigure(nextFigure);
// Start with the new figure's layout, then only preserve user-interaction
// values (dragmode, xaxis, yaxis) from the previous layout.
// We don't want to preserve other properties like `shapes` from the previous
// layout, as they should be fully controlled by the figure prop.
- setLayout((prev) => computeLayoutOnFigureChange(nextFigure, prev));
+ // When trace types change, axis settings are reset to avoid distortion (#5898).
+ setLayout((prev) => computeLayoutOnFigureChange(nextFigure, prevFig, prev));
}, [originalFigure, isScriptLoaded]);
const prevFigure = usePrevious(figure) ?? figure;