Bivariate distribution analysis
Explore the joint distribution of two numeric variables by showing where observations concentrate.
Plotly · Function
px.density_heatmap creates an interactive 2D histogram, grouping x/y observations into bins and coloring each tile by count or an aggregation of z. Bin resolution and color mapping are configurable.
Pass tabular data and identify the two dimensions with x and y. Plotly groups points into rectangular bins and colors each bin by count by default; supplying z with histfunc changes the aggregation, while options such as nbinsx, nbinsy, and color_continuous_scale control presentation.
px.density_heatmap(data_frame=None, x=None, y=None, z=None, histfunc=None, nbinsx=None, nbinsy=None, color_continuous_scale=None, **kwargs)import plotly.express as px
df = px.data.tips()
fig = px.density_heatmap(
df,
x="total_bill",
y="tip",
nbinsx=20,
nbinsy=20,
color_continuous_scale="Viridis",
)
fig.show()Creates an interactive 2D histogram from the built-in tips dataset, using explicit bin counts and a continuous color scale.
Explore the joint distribution of two numeric variables by showing where observations concentrate.
Summarize a third numeric field within x/y regions using z and histfunc.
Tune visual granularity for a dense dataset with separate horizontal and vertical bin counts.
It creates a 2D histogram, also called a density heatmap. Points are grouped into bins by their x and y coordinates, and each tile is colored using a count by default.
Follow-up: How would you encode a value other than point frequency? Provide z and select the desired aggregation with histfunc.
Use nbinsx and nbinsy to control the number of bins along the two axes. Use color_continuous_scale to select the color mapping for the resulting values.
Follow-up: How could you show the same distribution beside the main chart? Add marginal_x or marginal_y with a supported marginal plot type.
The function accepts long-form data and also supports wide-form and mixed-form inputs because it is one of Plotly Express's 2D-Cartesian functions. With a DataFrame, column names can be supplied as strings.
Follow-up: What should you verify when using wide-form input? Confirm that the inferred index and columns represent the intended axes and variables.
px.density_contourpx.histogrammarginal_xmarginal_ynbinsxnbinsycolor_continuous_scalehistfunc