|
| 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) |
0 commit comments