-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Px density docs #2362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Px density docs #2362
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ jupyter: | |
extension: .md | ||
format_name: markdown | ||
format_version: '1.2' | ||
jupytext_version: 1.3.0 | ||
jupytext_version: 1.3.1 | ||
kernelspec: | ||
display_name: Python 3 | ||
language: python | ||
|
@@ -20,7 +20,7 @@ jupyter: | |
name: python | ||
nbconvert_exporter: python | ||
pygments_lexer: ipython3 | ||
version: 3.7.5 | ||
version: 3.6.8 | ||
plotly: | ||
description: How to make 2D Histograms in Python with Plotly. | ||
display_as: statistical | ||
|
@@ -33,6 +33,69 @@ jupyter: | |
thumbnail: thumbnail/histogram2d.jpg | ||
--- | ||
|
||
## 2D Histograms or Density Heatmaps | ||
|
||
A 2D histogram, also known as a density heatmap, is the 2-dimensional generalization of a [histogram](/python/histograms/) which resembles a [heatmap](/python/heatmaps/) but is computed by grouping a set of points specifed by their `x` and `y` coordinates into bins, and applying an aggregation function such as `count` or `sum` (if `z` is provided) to compute the color of the tile representing the bin. This kind of visualization (and the related [2D histogram contour, or density contour](https://plotly.com/python/2d-histogram-contour/)) is often used to manage over-plotting, or situations where showing large data sets as [scatter plots](/python/line-and-scatter/) would result in points overlapping each other and hiding patterns. For data sets of more than a few thousand points, a better approach than the ones listed here would be to [use Plotly with Datashader](/python/datashader/) to precompute the aggregations before displaying the data with Plotly. | ||
|
||
## Density Heatmaps with Plotly Express | ||
|
||
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on "tidy" data](/python/px-arguments/) and produces [easy-to-style figures](/python/styling-plotly-express/). The Plotly Express function `density_heatmap()` can be used to produce density heatmaps. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's not mention tidy data here since this will change soon... one less file to change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually it'll be easier to search and replace if I leave this "standard language" in place as-is ;) |
||
|
||
```python | ||
import plotly.express as px | ||
df = px.data.tips() | ||
|
||
fig = px.density_heatmap(df, x="total_bill", y="tip") | ||
fig.show() | ||
``` | ||
|
||
The number of bins can be controlled with `nbinsx` and `nbinsy` and the [color scale](/python/colorscales/) with `color_continuous_scale`. | ||
|
||
```python | ||
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() | ||
``` | ||
|
||
Marginal plots can be added to visualize the 1-dimensional distributions of the two variables. Here we use a marginal [`histogram`](/python/histograms/). Other allowable values are `violin`, `box` and `rug`. | ||
|
||
```python | ||
import plotly.express as px | ||
df = px.data.tips() | ||
|
||
fig = px.density_heatmap(df, x="total_bill", y="tip", marginal_x="histogram", marginal_y="histogram") | ||
fig.show() | ||
``` | ||
|
||
Density heatmaps can also be [faceted](/python/facet-plots/): | ||
|
||
```python | ||
import plotly.express as px | ||
df = px.data.tips() | ||
|
||
fig = px.density_heatmap(df, x="total_bill", y="tip", facet_row="sex", facet_col="smoker") | ||
fig.show() | ||
``` | ||
|
||
### Other aggregation functions than `count` | ||
|
||
By passing in a `z` value and a `histfunc`, density heatmaps can perform basic aggregation operations. Here we show average Sepal Length grouped by Petal Length and Petal Width for the Iris dataset. | ||
|
||
```python | ||
import plotly.express as px | ||
df = px.data.iris() | ||
|
||
fig = px.density_heatmap(df, x="petal_length", y="petal_width", z="sepal_length", histfunc="avg") | ||
fig.show() | ||
``` | ||
|
||
### 2D Histograms with Graph Objects | ||
|
||
To build this kind of plot without using Plotly Express, we can use the `go.Histogram2d` class. | ||
|
||
|
||
### 2D Histogram of a Bivariate Normal Distribution ### | ||
|
||
```python | ||
|
Uh oh!
There was an error while loading. Please reload this page.