Append series to an empty figure
Building a chart incrementally
Plotly · Module
Adds one trace to a Plotly graph-object figure and returns the modified figure. It also supports selecting a subplot and, where configured, its secondary y-axis.
fig.add_trace accepts a graph-object trace or a trace dictionary, appends it to the figure, and returns the figure so calls can be chained. For subplot figures, row, col, and secondary_y select the destination axis.
fig.add_trace(trace, row=None, col=None, secondary_y=None, exclude_empty_subplots=False)| Name | Type | Description |
|---|---|---|
trace | BaseTraceType or dict | Trace object or dictionary to add. A trace object may be a graph-object instance such as Scatter or Bar; a dictionary specifies a trace type and its properties. |
row | int or 'all' or None | Subplot row index, starting at 1; valid for figures created with make_subplots. |
col | int or 'all' or None | Subplot column index, starting at 1; valid for figures created with make_subplots. |
secondary_y | bool or None | Whether to add the trace to the secondary y-axis of the selected subplot. |
exclude_empty_subplots | bool | If true, prevents adding the trace to subplots that do not already contain traces. |
The modified plotly.graph_objects.Figure.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, 2], mode="lines+markers"))
fig.show()Creates a figure, adds a scatter trace, and displays the resulting interactive chart.
Building a chart incrementally
Composing dashboards
Programmatic chart generation
It adds one graph-object trace to a figure and returns the figure. The trace can be a graph-objects instance such as Scatter or Bar, or a dictionary describing the trace.
Follow-up: How would you target a subplot? Pass row and col to add_trace; secondary_y can select the secondary y-axis when the subplot supports it.
Use row and col, with indexes beginning at 1, to place the trace in a chosen subplot. These arguments are meaningful for figures created with make_subplots.
Follow-up: What happens if the figure is not a subplot figure? The subplot-position arguments are not valid for that figure configuration.
A trace dictionary uses its type property to select the trace class, and its remaining properties become constructor arguments. If type is absent, Plotly assumes scatter.
Follow-up: When predictable trace construction matters, provide an explicit type or use a graph-object class such as go.Bar.
Figure.add_tracesFigure.add_scatterFigure.add_barplotly.subplots.make_subplots