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

Plotly · Class

go.Histogram2dContour

go.Histogram2dContour creates a graph-object trace that displays paired x/y observations as a two-dimensional density contour plot. Add it to a Figure and customize its color scale and trace properties.

How it works

Histogram2dContour bins paired x and y observations into a two-dimensional frequency grid, then renders the grid as filled contour regions. Configure visual encoding with options such as colorscale and reversescale, and place the trace in a Figure or add it alongside other traces.

Syntax

go.Histogram2dContour(arg=None, autoplay=None, ..., x=None, y=None)

Parameters

NameTypeDescription
argdictValues used to construct the trace; the page's constructor reference represents this as the optional arg parameter.
xarray-likeInput values along the x dimension used to form the two-dimensional histogram.
yarray-likeInput values along the y dimension used to form the two-dimensional histogram.
colorscalestr or listColor scale used to render contour density.
reversescaleboolWhether to reverse the selected color scale.

Returns

A plotly.graph_objects.Histogram2dContour trace object.

Key points

  • Use the graph-objects interface when direct trace construction is preferable to Plotly Express.
  • Pass a Histogram2dContour instance directly to go.Figure to create a figure containing the contour trace.
  • Provide paired x and y observations to describe the two-dimensional distribution.

Example: Basic 2D histogram contour

import plotly.graph_objects as go
import numpy as np

rng = np.random.default_rng(1)
x = rng.uniform(-1, 1, 500)
y = rng.uniform(-1, 1, 500)
fig = go.Figure(go.Histogram2dContour(
    x=x,
    y=y,
    colorscale="Blues"
))
fig.show()

Creates a 2D density contour from paired x and y samples.

When to use it

Visualize bivariate density

You have two continuous variables and want to show where observations concentrate across their joint range.

Reduce overplotting

A scatter plot contains too many overlapping points to reveal concentration clearly.

Compose interactive figures

You need a graph-object trace that can be placed beside or beneath other Plotly traces.

Interview questions

QWhat does go.Histogram2dContour represent?

Histogram2dContour is a graph-object trace for displaying the density of paired x and y observations as contours. A basic figure can be created with go.Figure(go.Histogram2dContour(x=x, y=y)).

Follow-up: How would you add another visualization? Add the contour with fig.add_trace(...) and configure the additional trace independently.

QWhich data does the trace require?

The trace accepts x and y sample arrays and estimates their two-dimensional distribution for rendering. Its appearance can be changed with properties such as colorscale.

Follow-up: How can the color direction be inverted? Set reversescale=True on the contour trace.

QHow is the trace created in Python?

The trace can be used with graph objects instead of Plotly Express. Construct it directly and pass it to go.Figure, then display the resulting figure with fig.show().

Follow-up: How can it be combined with a scatter plot? Add a go.Scatter trace to the same Figure, using compatible x and y coordinates.

  • go.Histogram2d
  • go.Histogram
  • go.Scatter
  • go.Figure

Source: 2D Histogram Contour in Python - Plotly. Checked 2026-10-15.