Hexbin Map in Python
How to make a map with Hexagonal Binning of data in Python with Plotly.
Plotly is known for its graphing libraries, but it's also a broader ecosystem for building interactive data apps, AI data analysis tools, and deploying and hosting Dash apps. Explore the full set of tools and platforms designed to help turn data into action. Discover Plotly.
Simple Count Hexbin¶
This page details the use of a figure factory. For more examples with Choropleth maps, see this page.
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = ff.create_hexbin_map(
data_frame=df, lat="centroid_lat", lon="centroid_lon",
nx_hexagon=10, opacity=0.9, labels={"color": "Point Count"},
)
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()
Count Hexbin with Minimum Count and Opacity¶
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = ff.create_hexbin_map(
data_frame=df, lat="centroid_lat", lon="centroid_lon",
nx_hexagon=10, opacity=0.5, labels={"color": "Point Count"},
min_count=1,
)
fig.show()
Display the Underlying Data¶
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = ff.create_hexbin_map(
data_frame=df, lat="centroid_lat", lon="centroid_lon",
nx_hexagon=10, opacity=0.5, labels={"color": "Point Count"},
min_count=1, color_continuous_scale="Viridis",
show_original_data=True,
original_data_marker=dict(size=4, opacity=0.6, color="deeppink")
)
fig.show()
Compute the Mean Value per Hexbin¶
import plotly.figure_factory as ff
import plotly.express as px
import numpy as np
df = px.data.carshare()
fig = ff.create_hexbin_map(
data_frame=df, lat="centroid_lat", lon="centroid_lon",
nx_hexagon=10, opacity=0.9, labels={"color": "Average Peak Hour"},
color="peak_hour", agg_func=np.mean, color_continuous_scale="Icefire", range_color=[0,23]
)
fig.show()
Compute the Sum Value per Hexbin¶
import plotly.figure_factory as ff
import plotly.express as px
import numpy as np
df = px.data.carshare()
fig = ff.create_hexbin_map(
data_frame=df, lat="centroid_lat", lon="centroid_lon",
nx_hexagon=10, opacity=0.9, labels={"color": "Summed Car.Hours"},
color="car_hours", agg_func=np.sum, color_continuous_scale="Magma"
)
fig.show()
Hexbin with Animation¶
import plotly.figure_factory as ff
import plotly.express as px
import numpy as np
np.random.seed(0)
N = 500
n_frames = 12
lat = np.concatenate([
np.random.randn(N) * 0.5 + np.cos(i / n_frames * 2 * np.pi) + 10
for i in range(n_frames)
])
lon = np.concatenate([
np.random.randn(N) * 0.5 + np.sin(i / n_frames * 2 * np.pi)
for i in range(n_frames)
])
frame = np.concatenate([
np.ones(N, int) * i for i in range(n_frames)
])
fig = ff.create_hexbin_map(
lat=lat, lon=lon, nx_hexagon=15, animation_frame=frame,
color_continuous_scale="Cividis", labels={"color": "Point Count", "frame": "Period"},
opacity=0.5, min_count=1,
show_original_data=True, original_data_marker=dict(opacity=0.6, size=4, color="deeppink")
)
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.layout.sliders[0].pad.t=20
fig.layout.updatemenus[0].pad.t=40
fig.show()
Reference¶
For more info on Plotly maps, see: https://plotly.com/python/maps.
For more info on using colorscales with Plotly see: https://plotly.com/python/heatmap-and-contour-colorscales/
For more info on ff.create_hexbin_map(), see the full function reference
What About Dash?¶
Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash at https://dash.plot.ly/installation.
Everywhere in this page that you see fig.show(), you can display the same figure in a Dash application by passing it to the figure argument of the Graph component from the built-in dash_core_components package like this:
import plotly.graph_objects as go # or plotly.express as px
fig = go.Figure() # or any Plotly Express function e.g. px.bar(...)
# fig.add_trace( ... )
# fig.update_layout( ... )
from dash import Dash, dcc, html
app = Dash()
app.layout = html.Div([
dcc.Graph(figure=fig)
])
app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter