Skip to content

Commit ff9e9c0

Browse files
Merge pull request #2638 from plotly/hexbin
Finishing up Hexbin Mapbox PR
2 parents 25a77f3 + 6ecef8b commit ff9e9c0

File tree

6 files changed

+866
-2
lines changed

6 files changed

+866
-2
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1111
- `facet_row_spacing` and `facet_col_spacing` added to Plotly Express cartesian 2d functions ([#2614](https://github.com/plotly/plotly.py/pull/2614))
1212
- `base` added to Plotly Express `bar` and `bar_polar` functions
1313
- `plotly.express.timeline()` added as an official alternative to `plotly.figure_factories.create_gantt()`
14+
- `create_hexbin_mapbox()` added to Figure Factories, with thanks to [@RenaudLN](https://github.com/RenaudLN) for the contribution!
1415

1516
### Fixed
1617

@@ -22,8 +23,6 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2223
- `plotly.express.imshow` now uses data frame index and columns names and values to populate axis parameters by default ([#2539](https://github.com/plotly/plotly.py/pull/2539))
2324

2425

25-
26-
2726
## [4.8.2] - 2020-06-26
2827

2928
### Updated
@@ -43,6 +42,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4342
- `px.line` now sets `line_group=<variable>` in wide mode by default ([#2599](https://github.com/plotly/plotly.py/pull/2599))
4443
- Corrected some regex warnings ([#2577](https://github.com/plotly/plotly.py/pull/2577)), with thanks to [@georgevdd](https://github.com/georgevdd) for the contribution!
4544

45+
4646
## [4.8.1] - 2020-05-28
4747

4848
### Fixed

doc/python/figure-factories.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The following types of plots are still difficult to create with Graph Objects or
4141

4242
* [Annotated Heatmaps](/python/annotated-heatmap/)
4343
* [Dendrograms](/python/dendrogram/)
44+
* [Hexagonal Binning Tile Map](/python/hexbin-mapbox/)
4445
* [Quiver Plots](/python/quiver-plots/)
4546
* [Streamline Plots](/python/streamline-plots/)
4647
* [Tables](/python/figure-factory-table/)

doc/python/hexbin-mapbox.md

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.7.7
24+
plotly:
25+
description: How to make a map with Hexagonal Binning of data in Python with Plotly.
26+
display_as: maps
27+
language: python
28+
layout: base
29+
name: Hexbin Mapbox
30+
order: 13
31+
page_type: u-guide
32+
permalink: python/hexbin-mapbox/
33+
thumbnail: thumbnail/hexbin_mapbox.jpg
34+
---
35+
36+
#### Simple Count Hexbin
37+
38+
This page details the use of a [figure factory](/python/figure-factories/). For more examples with Choropleth maps, see [this page](/python/choropleth-maps/).
39+
40+
In order to use mapbox styles that require a mapbox token, set the token with `plotly.express`. You can also use styles that do not require a mapbox token. See more information on [this page](/python/mapbox-layers/).
41+
42+
```python
43+
import plotly.figure_factory as ff
44+
import plotly.express as px
45+
46+
px.set_mapbox_access_token(open(".mapbox_token").read())
47+
df = px.data.carshare()
48+
49+
fig = ff.create_hexbin_mapbox(
50+
data_frame=df, lat="centroid_lat", lon="centroid_lon",
51+
nx_hexagon=10, opacity=0.9, labels={"color": "Point Count"},
52+
)
53+
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
54+
fig.show()
55+
```
56+
57+
#### Count Hexbin with Minimum Count and Opacity
58+
59+
```python
60+
import plotly.figure_factory as ff
61+
import plotly.express as px
62+
63+
px.set_mapbox_access_token(open(".mapbox_token").read())
64+
df = px.data.carshare()
65+
66+
fig = ff.create_hexbin_mapbox(
67+
data_frame=df, lat="centroid_lat", lon="centroid_lon",
68+
nx_hexagon=10, opacity=0.5, labels={"color": "Point Count"},
69+
min_count=1,
70+
)
71+
fig.show()
72+
```
73+
74+
#### Display the Underlying Data
75+
76+
```python
77+
import plotly.figure_factory as ff
78+
import plotly.express as px
79+
80+
px.set_mapbox_access_token(open(".mapbox_token").read())
81+
df = px.data.carshare()
82+
83+
fig = ff.create_hexbin_mapbox(
84+
data_frame=df, lat="centroid_lat", lon="centroid_lon",
85+
nx_hexagon=10, opacity=0.5, labels={"color": "Point Count"},
86+
min_count=1, color_continuous_scale="Viridis",
87+
show_original_data=True,
88+
original_data_marker=dict(size=4, opacity=0.6, color="deeppink")
89+
)
90+
fig.show()
91+
```
92+
93+
#### Compute the Mean Value per Hexbin
94+
95+
```python
96+
import plotly.figure_factory as ff
97+
import plotly.express as px
98+
import numpy as np
99+
100+
px.set_mapbox_access_token(open(".mapbox_token").read())
101+
df = px.data.carshare()
102+
103+
fig = ff.create_hexbin_mapbox(
104+
data_frame=df, lat="centroid_lat", lon="centroid_lon",
105+
nx_hexagon=10, opacity=0.9, labels={"color": "Average Peak Hour"},
106+
color="peak_hour", agg_func=np.mean, color_continuous_scale="Icefire", range_color=[0,23]
107+
)
108+
fig.show()
109+
```
110+
111+
#### Compute the Sum Value per Hexbin
112+
113+
```python
114+
import plotly.figure_factory as ff
115+
import plotly.express as px
116+
import numpy as np
117+
118+
px.set_mapbox_access_token(open(".mapbox_token").read())
119+
df = px.data.carshare()
120+
121+
fig = ff.create_hexbin_mapbox(
122+
data_frame=df, lat="centroid_lat", lon="centroid_lon",
123+
nx_hexagon=10, opacity=0.9, labels={"color": "Summed Car.Hours"},
124+
color="car_hours", agg_func=np.sum, color_continuous_scale="Magma"
125+
)
126+
fig.show()
127+
```
128+
129+
#### Hexbin with Animation
130+
131+
```python
132+
import plotly.figure_factory as ff
133+
import plotly.express as px
134+
import numpy as np
135+
136+
px.set_mapbox_access_token(open(".mapbox_token").read())
137+
np.random.seed(0)
138+
139+
N = 500
140+
n_frames = 12
141+
lat = np.concatenate([
142+
np.random.randn(N) * 0.5 + np.cos(i / n_frames * 2 * np.pi) + 10
143+
for i in range(n_frames)
144+
])
145+
lon = np.concatenate([
146+
np.random.randn(N) * 0.5 + np.sin(i / n_frames * 2 * np.pi)
147+
for i in range(n_frames)
148+
])
149+
frame = np.concatenate([
150+
np.ones(N, int) * i for i in range(n_frames)
151+
])
152+
153+
fig = ff.create_hexbin_mapbox(
154+
lat=lat, lon=lon, nx_hexagon=15, animation_frame=frame,
155+
color_continuous_scale="Cividis", labels={"color": "Point Count", "frame": "Period"},
156+
opacity=0.5, min_count=1,
157+
show_original_data=True, original_data_marker=dict(opacity=0.6, size=4, color="deeppink")
158+
)
159+
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
160+
fig.layout.sliders[0].pad.t=20
161+
fig.layout.updatemenus[0].pad.t=40
162+
fig.show()
163+
```
164+
165+
#### Reference
166+
167+
For more info on Plotly maps, see: https://plotly.com/python/maps.<br> For more info on using colorscales with Plotly see: https://plotly.com/python/heatmap-and-contour-colorscales/ <br>For more info on `ff.create_annotated_heatmap()`, see the [full function reference](https://plotly.com/python-api-reference/generated/plotly.figure_factory.create_hexbin_mapbox.html#plotly.figure_factory.create_hexbin_mapbox)

packages/python/plotly/plotly/figure_factory/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929

3030
if optional_imports.get_module("pandas") is not None:
3131
from plotly.figure_factory._county_choropleth import create_choropleth
32+
from plotly.figure_factory._hexbin_mapbox import create_hexbin_mapbox
3233
else:
3334

3435
def create_choropleth(*args, **kwargs):
3536
raise ImportError("Please install pandas to use `create_choropleth`")
3637

38+
def create_hexbin_mapbox(*args, **kwargs):
39+
raise ImportError("Please install pandas to use `create_hexbin_mapbox`")
40+
3741

3842
if optional_imports.get_module("skimage") is not None:
3943
from plotly.figure_factory._ternary_contour import create_ternary_contour
@@ -53,6 +57,7 @@ def create_ternary_contour(*args, **kwargs):
5357
"create_distplot",
5458
"create_facet_grid",
5559
"create_gantt",
60+
"create_hexbin_mapbox",
5661
"create_ohlc",
5762
"create_quiver",
5863
"create_scatterplotmatrix",

0 commit comments

Comments
 (0)