.Histogram2d()
Anonymous contributor
Published Nov 30, 2024
Contribute to Docs
The .Histogram2d()
method in Plotly’s graph_objects
module creates a 2D histogram to display the joint distribution of two variables. It uses a grid where color intensity represents the count or aggregated values within each cell, making it useful for visualizing relationships and density in bivariate data.
Syntax
plotly.graph_objects.Histogram2d(x=None, y=None, nbinsx=None, nbinsy=None, colorscale=None, ...)
x
: Input data for the x-axis, representing the values plotted along the horizontal axis.y
: Input data for the y-axis, representing the values plotted along the vertical axis.nbinsx
(Optional): The number of bins (intervals) to use for the x-axis. If not specified (None
), Plotly automatically calculates an appropriate number of bins based on the data.nbinsy
(Optional): The number of bins (intervals) to use for the y-axis. If not specified (None
), Plotly automatically calculates an appropriate number of bins based on the data.colorscale
(Optional): Defines the color scale for bin intensities (e.g.,'Viridis'
,'Blues'
). You can also define custom color scales.
Note: The ellipsis in the syntax (…) indicates that there are additional optional parameters beyond those listed here to customize the scatter plot on polar axes.
Example
The following example showcases the use of the .Histogram2d()
:
import plotly.graph_objects as go# Sample datax = [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 9, 10, 10]y = [10, 9, 8, 7, 6, 6, 5, 5, 4, 3, 2, 2, 1, 1, 0]# Create the 2D histogramfig = go.Figure(data=go.Histogram2d(x=x,y=y,colorscale='Blues', # Color scalecolorbar=dict(title="Frequency"), # Customize the color bar))# Add layout detailsfig.update_layout(title="Example of a 2D Histogram",xaxis_title="X Axis",yaxis_title="Y Axis",template="plotly_white" # Use a clean template)# Display the figurefig.show()
This example demonstrates how to use plotly.graph_objects.Histogram2d
to create a two-dimensional histogram and visualize it as a heatmap.
The above code generates 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