Skip to content

Commit b146002

Browse files
Merge pull request #2362 from plotly/px_density_docs
Px density docs
2 parents 711324a + 6e0c2bd commit b146002

File tree

2 files changed

+132
-5
lines changed

2 files changed

+132
-5
lines changed

doc/python/2D-Histogram.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.3.0
9+
jupytext_version: 1.3.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.5
23+
version: 3.6.8
2424
plotly:
2525
description: How to make 2D Histograms in Python with Plotly.
2626
display_as: statistical
@@ -33,6 +33,69 @@ jupyter:
3333
thumbnail: thumbnail/histogram2d.jpg
3434
---
3535

36+
## 2D Histograms or Density Heatmaps
37+
38+
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 specified 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.
39+
40+
## Density Heatmaps with Plotly Express
41+
42+
[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.
43+
44+
```python
45+
import plotly.express as px
46+
df = px.data.tips()
47+
48+
fig = px.density_heatmap(df, x="total_bill", y="tip")
49+
fig.show()
50+
```
51+
52+
The number of bins can be controlled with `nbinsx` and `nbinsy` and the [color scale](/python/colorscales/) with `color_continuous_scale`.
53+
54+
```python
55+
import plotly.express as px
56+
df = px.data.tips()
57+
58+
fig = px.density_heatmap(df, x="total_bill", y="tip", nbinsx=20, nbinsy=20, color_continuous_scale="Viridis")
59+
fig.show()
60+
```
61+
62+
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`.
63+
64+
```python
65+
import plotly.express as px
66+
df = px.data.tips()
67+
68+
fig = px.density_heatmap(df, x="total_bill", y="tip", marginal_x="histogram", marginal_y="histogram")
69+
fig.show()
70+
```
71+
72+
Density heatmaps can also be [faceted](/python/facet-plots/):
73+
74+
```python
75+
import plotly.express as px
76+
df = px.data.tips()
77+
78+
fig = px.density_heatmap(df, x="total_bill", y="tip", facet_row="sex", facet_col="smoker")
79+
fig.show()
80+
```
81+
82+
### Other aggregation functions than `count`
83+
84+
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.
85+
86+
```python
87+
import plotly.express as px
88+
df = px.data.iris()
89+
90+
fig = px.density_heatmap(df, x="petal_length", y="petal_width", z="sepal_length", histfunc="avg")
91+
fig.show()
92+
```
93+
94+
### 2D Histograms with Graph Objects
95+
96+
To build this kind of plot without using Plotly Express, we can use the `go.Histogram2d` class.
97+
98+
3699
### 2D Histogram of a Bivariate Normal Distribution ###
37100

38101
```python

doc/python/2d-histogram-contour.md

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.6.8
2424
plotly:
2525
description: How to make 2D Histogram Contour plots in Python with Plotly.
2626
display_as: statistical
@@ -30,9 +30,73 @@ jupyter:
3030
order: 12
3131
page_type: u-guide
3232
permalink: python/2d-histogram-contour/
33+
redirect_from: python/2d-density-plots/
3334
thumbnail: thumbnail/hist2dcontour.png
3435
---
3536

37+
## 2D Histogram Contours or Density Contours
38+
39+
A 2D histogram contour plot, also known as a density contour plot, is a 2-dimensional generalization of a [histogram](/python/histograms/) which resembles a [contour plot](/python/contour-plots/) but is computed by grouping a set of points specified 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 value to be used to compute contours. This kind of visualization (and the related [2D histogram, or density heatmap](/python/2d-histogram/)) 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.
40+
41+
## Density Contours with Plotly Express
42+
43+
[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_contour()` can be used to produce density contours.
44+
45+
```python
46+
import plotly.express as px
47+
df = px.data.tips()
48+
49+
fig = px.density_contour(df, x="total_bill", y="tip")
50+
fig.show()
51+
```
52+
53+
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`.
54+
55+
```python
56+
import plotly.express as px
57+
df = px.data.tips()
58+
59+
fig = px.density_contour(df, x="total_bill", y="tip", marginal_x="histogram", marginal_y="histogram")
60+
fig.show()
61+
```
62+
63+
Density contours can also be [faceted](/python/facet-plots/) and [discretely colored](/python/discrete-color/):
64+
65+
```python
66+
import plotly.express as px
67+
df = px.data.tips()
68+
69+
fig = px.density_contour(df, x="total_bill", y="tip", facet_col="sex", color="smoker")
70+
fig.show()
71+
```
72+
73+
Plotly Express density contours can be [continuously-colored](/python/colorscales/) and labeled:
74+
75+
```python
76+
import plotly.express as px
77+
df = px.data.tips()
78+
79+
fig = px.density_contour(df, x="total_bill", y="tip")
80+
fig.update_traces(contours_coloring="fill", contours_showlabels = True)
81+
fig.show()
82+
```
83+
84+
### Other aggregation functions than `count`
85+
86+
By passing in a `z` value and a `histfunc`, density contours can perform basic aggregation operations. Here we show average Sepal Length grouped by Petal Length and Petal Width for the Iris dataset.
87+
88+
```python
89+
import plotly.express as px
90+
df = px.data.iris()
91+
92+
fig = px.density_contour(df, x="petal_length", y="petal_width", z="sepal_length", histfunc="avg")
93+
fig.show()
94+
```
95+
96+
### 2D Histograms with Graph Objects
97+
98+
To build this kind of plot without using Plotly Express, we can use the `go.Histogram2d` class.
99+
36100
#### Basic 2D Histogram Contour
37101

38102
```python

0 commit comments

Comments
 (0)