Visualize bivariate density
You have two continuous variables and want to show where observations concentrate across their joint range.
Plotly · Class
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.
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.
go.Histogram2dContour(arg=None, autoplay=None, ..., x=None, y=None)| Name | Type | Description |
|---|---|---|
arg | dict | Values used to construct the trace; the page's constructor reference represents this as the optional arg parameter. |
x | array-like | Input values along the x dimension used to form the two-dimensional histogram. |
y | array-like | Input values along the y dimension used to form the two-dimensional histogram. |
colorscale | str or list | Color scale used to render contour density. |
reversescale | bool | Whether to reverse the selected color scale. |
A plotly.graph_objects.Histogram2dContour trace object.
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.
You have two continuous variables and want to show where observations concentrate across their joint range.
A scatter plot contains too many overlapping points to reveal concentration clearly.
You need a graph-object trace that can be placed beside or beneath other Plotly traces.
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.
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.
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.Histogram2dgo.Histogramgo.Scattergo.Figure