.density_heatmap()
The .density_heatmap()
function generates a heatmap where color intensity represents the concentration of data points in each bin. It is useful for visualizing relationships between two variables in large datasets, identifying patterns, clusters, and outliers. The function automatically bins data along both axes and uses color to indicate the count or density within each bin.
Syntax
px.density_heatmap(data_frame=None, x=None, y=None, nbinsx=None, nbinsy=None, color_continuous_scale=None, ...)
data_frame
: DataFrame or array-like object containing the datax
: Column name or array for the x-axisy
: Column name or array for the y-axisnbinsx
: Number of bins along the x-axis for grouping datanbinsy
: Number of bins along the y-axis for grouping datacolor_continuous_scale
: Color scale used for the heatmap
Note: The ellipsis (
...
) indicates that there can be additional optional parameters beyond those listed here.
Example
The following code creates a density heatmap showing the relationship between sepal width and length in the iris dataset. The intensity of color represents how many data points fall within each bin, making it easy to identify where measurements are most concentrated:
import plotly.express as px# Load the built-in iris datasetdf = px.data.iris()# Create a basic density heatmapfig = px.density_heatmap(df,x="sepal_width",y="sepal_length",color_continuous_scale="Viridis",nbinsx=15,nbinsy=15,title="Sepal Width vs Length Density")# Update layout for better readabilityfig.update_layout(xaxis_title="Sepal Width (cm)",yaxis_title="Sepal Length (cm)",coloraxis_colorbar_title="Count")fig.show()
The above code results in the following output:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Plotly on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours