plotly.express and plotly.graph_objects mutation boundary: do Express defaults survive graph_objects updates?
0 reputation · 27 Dec 2021, 12:35 UTC
0 reputation · 27 Dec 2021, 12:35 UTC
When a figure is constructed with plotly.express and then modified using plotly.graph_objects methods such as update_layout, update_traces, or add_trace, the two APIs operate on the same underlying figure object. Express returns a graph_objects.Figure and only exposes a subset of layout and trace properties through keyword arguments, so direct mutation is a common path for advanced styling. However, it is unclear whether those graph_objects mutations preserve the Express-specific defaults that later Express revision functions expect.
Because Express coverage of graph_objects features varies by Plotly version, and because Express kwargs do not map one-to-one to every underlying property, the boundary between Express-managed defaults and explicit graph_objects updates can become ambiguous. This raises a design question for figures that are built incrementally across both APIs.
Do graph_objects mutations on an Express figure preserve Express defaults that subsequent Express functions rely on, such as category ordering or color sequences? Is there a documented way to distinguish Express-injected defaults from user-applied graph_objects properties in the figure schema? Which Plotly versions guarantee that mixing these mutation paths does not silently invalidate Express-specific behavior?
No. Do not rely on graph_objects mutations to preserve Express defaults that a later Express call expects. plotly.express returns the same plotly.graph_objects.Figure object, and update_layout, update_traces and add_trace operate on that object by setting or overwriting ordinary figure properties. There is no documented Express marker inside the schema that separates Express-injected values from user-applied graph_objects values, and no Plotly.py version should be treated as guaranteeing that mixing the two paths is always safe.
Confirmed at the API level: Express is a convenience layer over graph_objects, not a separate figure type. Its keyword arguments are a subset of what the underlying figure can represent, and they do not map one-to-one to every layout or trace property. graph_objects updates merge into or replace existing keys, so a later graph_objects write can change the exact keys an Express revision path reads.
Version-sensitive and therefore not something to assert universally: the precise merge behavior for nested layout objects, and whether a given default is stored explicitly in the figure JSON or computed from data and internal Express logic. Color sequences, category ordering and similar behavior may appear as explicit properties in one version or path, and may be derived rather than stored in another. Treat exact key names and outcomes as things to verify on your installed Plotly.py, not as a stable contract.
import plotly
import plotly.express as px
print(plotly.__version__)
fig = px.scatter(df, x='category', y='value', color='group')
before = fig.to_plotly_json()
fig.update_layout(legend_title_text='Group')
fig.update_traces(marker=dict(size=9))
after = fig.to_plotly_json()
fresh = px.scatter(df, x='category', y='value', color='group').to_plotly_json()
# Compare selected sections, for example layout and each trace, between
# before/after and after/fresh. Divergent keys are overwritten or recomputed.For a focused check, compare the sections you actually depend on, such as layout color-related keys, axis category-related keys and trace marker or line settings. If the mutated figure differs from a fresh Express figure in those sections, treat the Express default as not preserved for your use case.
No documented public API tags a property as Express-injected versus user-applied. The reliable approaches are snapshot testing, JSON diffs against a freshly built Express figure with identical inputs, and project-level conventions such as never mixing owners for the same key. Do not build production logic on undocumented internals.
None should be read as a guarantee. Plotly.py 4.x, 5.x and 6.x all expose a mutable figure model, but merge semantics and Express coverage vary by release. The one diagnostic detail that changes the recommendation is your exact installed plotly version; with that, plus a minimal repro, you can decide whether to pin, upgrade or restructure the figure-building code.
Bottom line: graph_objects mutations can coexist with Express figures, but they do not carry a preservation contract for Express-specific defaults. Make the boundary explicit in code, verify with JSON snapshots on your pinned version, and keep final styling in one place.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.