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

Plotly · Class

go.Bar

go.Bar creates an interactive bar-chart trace for Plotly graph-object figures. Configure coordinates with x and y, then add labels, hover text, orientation, and multi-trace layout behavior as needed.

How it works

go.Bar constructs a bar trace for a plotly.graph_objects.Figure. Supply category or numeric coordinates through x and y; additional trace properties control labels, hover content, orientation, styling, and grouping.

Syntax

plotly.graph_objects.Bar(arg=None, alignmentgroup=None, base=None, basesrc=None, cliponaxis=None, constraintext=None, customdata=None, customdatasrc=None, dx=None, dy=None, error_x=None, error_y=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, hovertemplate=None, hovertemplatesrc=None, hovertext=None, hovertextsrc=None, ids=None, idssrc=None, insidetextanchor=None, insidetextfont=None, legend=None, legendgroup=None, legendgrouptitle=None, legendrank=None, marker=None, meta=None, metasrc=None, name=None, offset=None, offsetgroup=None, offsetsrc=None, opacity=None, orientation=None, outsidetextfont=None, selected=None, selectedpoints=None, showlegend=None, stream=None, text=None, textangle=None, textfont=None, textinfo=None, textposition=None, texttemplate=None, texttemplatesrc=None, uid=None, uirevision=None, unselected=None, visible=None, width=None, x=None, x0=None, xaxis=None, xcalendar=None, xsrc=None, y=None, y0=None, yaxis=None, ycalendar=None, ysrc=None, zorder=None, **kwargs)

Parameters

NameTypeDescription
xarray-likeBar coordinates or category labels.
yarray-likeBar coordinates or numeric values.
textarray-likeText displayed or associated with each bar.
hovertextarray-likeCustom hover content for bars.
orientationstringControls the bar direction; use "h" for horizontal bars.

Returns

A plotly.graph_objects.Bar bar-trace object.

Key points

  • The class is intended for constructing bar charts through Plotly's graph-object API.
  • A basic trace pairs categorical labels in x with numeric measurements in y.
  • Set hovertext when each bar needs custom hover content.
  • Provide text and choose an automatic text position to display values on the bars.
  • Use the figure layout's barmode setting to configure how multiple bar traces are arranged.

Common mistakes

  • go.Bar is a graph-object trace; it must be used with the Plotly graph-objects figure workflow rather than treated as a complete figure.
  • To place value labels automatically on bars, set textposition to "auto" and provide text values.

Example: Basic bar chart

import plotly.graph_objects as go

fig = go.Figure(go.Bar(
    x=["A", "B", "C"],
    y=[20, 14, 23],
    text=[20, 14, 23],
    textposition="auto",
))
fig.show()

Creates a bar trace with categorical x-values and numeric y-values, then displays it.

When to use it

Categorical comparisons

Compare values across named categories such as products, animals, or regions.

Multi-series summaries

Present several series together and control whether bars are grouped or stacked.

Annotated charts

Show values beside marks or provide custom information on hover.

Horizontal bar charts

Rotate the chart layout so categories occupy the vertical axis.

Interview questions

QWhat is go.Bar used for?

go.Bar represents one bar-chart trace. It is typically passed to go.Figure, either directly or in the figure's data list, and fig.show() renders the result.

Follow-up: How would you add another series? Add another go.Bar trace with fig.add_trace() and configure the layout's barmode, such as "stack" or "group".

QHow do x, y, and orientation determine a bar chart?

Use x for the categories or horizontal coordinates and y for the corresponding values in a standard vertical bar chart. The arrays should describe matching observations.

Follow-up: How would you make horizontal bars? Swap the data roles as appropriate and set orientation="h"; the official horizontal-bar example supplies values through x and categories through y.

QHow can labels and hover text be added?

A bar trace can expose values directly on the marks by assigning a sequence to text and setting textposition="auto". Hover-specific annotations can instead be supplied with hovertext.

Follow-up: How can multiple traces be combined? Add each trace to the same figure and set a layout bar mode, for example fig.update_layout(barmode="stack").

  • plotly.graph_objects.Figure
  • fig.add_trace()
  • fig.update_layout()
  • barmode
  • plotly.express.bar

Source: Bar charts in Python - Plotly. Checked 2026-10-15.