Skip to content

Commit 8979720

Browse files
missing PX docs
1 parent 1505476 commit 8979720

File tree

2 files changed

+131
-5
lines changed

2 files changed

+131
-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 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.
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: 66 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
@@ -33,6 +33,69 @@ jupyter:
3333
thumbnail: thumbnail/hist2dcontour.png
3434
---
3535

36+
## 2D Histogram Contours or Density Contours
37+
38+
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 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 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.
39+
40+
## Density Contours 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_contour()` can be used to produce density contours.
43+
44+
```python
45+
import plotly.express as px
46+
df = px.data.tips()
47+
48+
fig = px.density_contour(df, x="total_bill", y="tip")
49+
fig.show()
50+
```
51+
52+
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`.
53+
54+
```python
55+
import plotly.express as px
56+
df = px.data.tips()
57+
58+
fig = px.density_contour(df, x="total_bill", y="tip", marginal_x="histogram", marginal_y="histogram")
59+
fig.show()
60+
```
61+
62+
Density contours can also be [faceted](/python/facet-plots/) and [discretely colored](/python/discrete-color/):
63+
64+
```python
65+
import plotly.express as px
66+
df = px.data.tips()
67+
68+
fig = px.density_contour(df, x="total_bill", y="tip", facet_col="sex", color="smoker")
69+
fig.show()
70+
```
71+
72+
Plotly Express density contours can be [continuously-colored](/python/colorscales/) and labeled:
73+
74+
```python
75+
import plotly.express as px
76+
df = px.data.tips()
77+
78+
fig = px.density_contour(df, x="total_bill", y="tip")
79+
fig.update_traces(contours_coloring="fill", contours_showlabels = True)
80+
fig.show()
81+
```
82+
83+
### Other aggregation functions than `count`
84+
85+
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.
86+
87+
```python
88+
import plotly.express as px
89+
df = px.data.iris()
90+
91+
fig = px.density_contour(df, x="petal_length", y="petal_width", z="sepal_length", histfunc="avg")
92+
fig.show()
93+
```
94+
95+
### 2D Histograms with Graph Objects
96+
97+
To build this kind of plot without using Plotly Express, we can use the `go.Histogram2d` class.
98+
3699
#### Basic 2D Histogram Contour
37100

38101
```python

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy