Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Plotly · Module

fig.add_bar

fig.add_bar() creates and appends a Plotly Bar trace to a figure. It supports incremental figure construction and can place the trace in a chosen subplot.

How it works

fig.add_bar() constructs a Bar trace from keyword arguments and appends it to a graph-object figure. It can also target a particular subplot with row and col, making it convenient for building figures incrementally.

Syntax

fig.add_bar(arg=None, **kwargs)

Key points

  • add_bar() belongs to Plotly's trace-specific helper-method family for constructing and adding chart traces.
  • Bar traces can be combined with other trace types while constructing one figure.
  • The general alternative is add_trace() with an explicitly constructed graph-object trace.
  • Use repeated calls when building a figure progressively rather than defining all traces at initialization.

Common mistakes

  • When targeting subplots, provide valid row and col coordinates; the figure must have a compatible subplot layout.

Example: Add bar traces to subplots

from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2)
fig.add_bar(x=["A", "B"], y=[2, 5], name="Sales", row=1, col=1)
fig.add_bar(x=["A", "B"], y=[3, 4], name="Returns", row=1, col=2)
fig.show()

Creates two bar traces and places each in a specified subplot.

When to use it

Sequential trace construction

Build a figure incrementally from an empty graph-object figure.

Subplot bar charts

Place bar data into a selected cell of a subplot grid.

Mixed chart types

Combine bars with lines or other trace types in one figure.

Interview questions

QWhat does fig.add_bar() do?

fig.add_bar() is a convenience method on graph-object figures that constructs and adds a Bar trace. It is equivalent in purpose to adding a go.Bar instance with add_trace().

Follow-up: How would you add a bar trace to a subplot? Pass row and col to fig.add_bar() after creating the subplot figure.

QHow does fig.add_bar() fit into Plotly's figure-building API?

The method accepts bar-trace properties such as x, y, and styling or grouping options, then adds the resulting trace to the figure. The documentation demonstrates y data together with subplot coordinates.

Follow-up: How can you combine chart types? Add other trace types, such as scatter traces, using their corresponding add_{trace} methods.

QWhen is incremental use of fig.add_bar() useful?

A figure can be assembled sequentially: start with an empty figure or subplot figure, then call add_bar() to append bar data. The resulting figure can be displayed with fig.show().

Follow-up: What is the lower-level alternative? Construct go.Bar(...) explicitly and pass it to fig.add_trace(...).

  • fig.add_trace
  • fig.add_scatter
  • go.Bar
  • make_subplots

Source: Creating and Updating Figures. Checked 2026-10-15.