Batch trace styling
Apply shared styling such as line width, marker color, or opacity to a group of traces.
Plotly · Module
Updates properties on one or more Plotly figure traces. Selectors and subplot coordinates let you limit which traces are changed.
Call this method on a Figure to apply trace-property changes to every trace matching the optional selector and subplot filters. Nested properties are merged recursively by default; pass overwrite=True to replace the existing nested property structure instead.
fig.update_traces(patch=None, selector=None, row=None, col=None, secondary_y=None, overwrite=False, **kwargs) → plotly.graph_objects._figure.Figure| Name | Type | Description |
|---|---|---|
patch | dict or None | Dictionary of property updates applied to every trace satisfying the selection criteria; defaults to None. |
selector | dict or None | Selection criteria used to choose which traces receive the update; defaults to None. |
row | int or None | Subplot row restricting which traces are updated; defaults to None. |
col | int or None | Subplot column restricting which traces are updated; defaults to None. |
secondary_y | bool or None | Whether the selected traces belong to a secondary y-axis; defaults to None. |
overwrite | bool | Whether to replace rather than recursively merge existing nested properties; defaults to False. |
kwargs | dict | Additional trace properties to update, supplied as keyword arguments. |
plotly.graph_objects._figure.Figure
import plotly.graph_objects as go
fig = go.Figure()
fig.add_scatter(y=[1, 3, 2], name="first")
fig.add_scatter(y=[2, 1, 4], name="second")
fig.update_traces(line=dict(width=3), selector=dict(type="scatter"))
fig.show()Creates two scatter traces and updates the line width of every scatter trace.
Apply shared styling such as line width, marker color, or opacity to a group of traces.
Change only traces matching a property such as type or name.
Modify traces belonging to one subplot in a multi-panel figure.
update_traces applies keyword property updates to all traces satisfying the supplied selection criteria. The selector can filter traces by properties, while row and col restrict updates to a subplot.
Follow-up: How would you update only the second scatter trace? Use a selector matching a trace property such as name, for example selector=dict(name="second").
The patch argument supplies a dictionary of property updates for matching traces. Keyword arguments are also accepted, and the method returns the Figure object so calls can be chained.
Follow-up: When would patch be preferable to direct keyword arguments? Use it when the update mapping is built dynamically or when you want to pass a reusable dictionary.
Nested updates are recursive by default, so unspecified nested properties remain in place. Setting overwrite=True changes this behavior and replaces the existing nested property structure with the supplied update.
Follow-up: What risk comes with overwrite=True? Any nested settings omitted from the replacement structure may be discarded.
Figure.update_layoutFigure.update_xaxesFigure.update_yaxesFigure.select_traces