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

Plotly · Class

go.Histogram

go.Histogram creates an interactive histogram trace from raw observations. Use x for vertical histograms and y for horizontal histograms.

How it works

go.Histogram is a graph-object trace that computes and displays the distribution of numeric observations. Provide values through x for a vertical histogram or through y for a horizontal one, then place the trace in a go.Figure.

Syntax

go.Histogram(x=values)

Returns

A Plotly graph-object histogram trace.

Key points

  • Use go.Histogram when Plotly Express is not a suitable starting point and direct graph-object control is preferred.
  • Create the trace with an x data sequence and pass it to a go.Figure.
  • Supplying y produces a horizontal histogram orientation.
  • Histogram-specific configuration is documented in Plotly's histogram reference section.

Common mistakes

  • Use y rather than x when the histogram should be horizontal.

Example: Basic histogram

import numpy as np
import plotly.graph_objects as go

np.random.seed(1)
x = np.random.randn(500)
fig = go.Figure(data=[go.Histogram(x=x)])
fig.show()

Creates and displays an interactive histogram from 500 normally distributed values.

When to use it

Explore distributions

Inspecting the distribution of a numeric sample.

Compare values along the y input

Showing a horizontal frequency distribution.

Compose custom figures

Building a graph-object figure with explicit trace configuration.

Interview questions

QWhat is go.Histogram used for, and how is it added to a figure?

go.Histogram is the generic graph-object class for creating a histogram trace. A typical figure constructs it with an x array and passes it to go.Figure.

Follow-up: How do you make the histogram horizontal? Pass the observations with the y argument instead of x.

QWhich data argument creates a vertical histogram?

Use x for the usual vertical orientation. The trace aggregates those observations into histogram bins and renders the resulting distribution interactively.

Follow-up: What is the corresponding input for a horizontal histogram? Use y, which changes the orientation while retaining histogram behavior.

QWhen should a histogram be preferred over a bar trace?

A histogram trace is appropriate when Plotly should perform the aggregation and counting from raw observations. If counts have already been computed, a bar trace is the more suitable representation.

Follow-up: Why not use a histogram for pre-aggregated values? Its purpose is to perform the binning and aggregation itself, whereas bars represent supplied values.

  • plotly.graph_objects.Figure
  • plotly.graph_objects.Bar
  • plotly.express.histogram
  • plotly.graph_objects.Histogram2d

Source: Histograms in Python. Checked 2026-10-15.