https://github.com/mapbox/mapboxgl-jupyter
These examples require the installation of the following python modules
pip install mapboxgl
pip install pandas
pip install pysal
import pysal.esda.mapclassify as mapclassify
import pandas as pd
import sys
import os
sys.path.append("..") # Adds higher directory to python modules path. Use to run this example locallay without a global pip install.
from mapboxgl.viz import *
from mapboxgl.utils import *
from mapboxgl.colors import *
df = pd.read_csv('../examples/points.csv').round(2)
df.head(3)
Avg Total Payments | Avg Covered Charges | Total Discharges | Avg Medicare Payments | admin1_id | Provider Id | admin2_id | lon | lat | |
---|---|---|---|---|---|---|---|---|---|
0 | 8749.03 | 35247.03 | 58.75 | 7678.21 | USA101 | 10001 | USA201069 | -85.36 | 31.22 |
1 | 6812.13 | 16451.09 | 28.96 | 5793.63 | USA101 | 10005 | USA201119 | -88.14 | 32.45 |
2 | 8197.24 | 36942.36 | 45.36 | 7145.96 | USA101 | 10006 | USA201077 | -87.68 | 34.79 |
MAPBOX_ACCESS_TOKEN
environment variable or copy/paste your token from https://www.mapbox.com/studio/account/tokens/¶# Must be a public token, starting with `pk`
token = os.getenv('MAPBOX_ACCESS_TOKEN')
# Generate data breaks and color stops from colorBrewer
color_breaks = mapclassify.Natural_Breaks(df['Avg Medicare Payments'].tolist(), k=8, initial=0).bins
color_stops = create_color_stops(color_breaks, colors='YlGnBu')
# Create the viz from the dataframe
viz = CircleViz(df_to_geojson(
df,
properties=['Avg Medicare Payments'],
precision=4),
access_token=token,
height='400px',
color_property = "Avg Medicare Payments",
color_stops = color_stops,
center = (-95, 40),
zoom = 3,
below_layer = 'waterway-label')
viz.show()
viz.label_property = "Avg Medicare Payments"
viz.show()
# Generate a new data domain breaks and a new color palette from colorBrewer2
color_breaks = mapclassify.Natural_Breaks(df['Avg Covered Charges'].tolist(), k=8, initial=0).bins
color_stops = create_color_stops(color_breaks, colors='YlOrRd')
# Show the viz
viz.data = df_to_geojson(df,
properties=['Avg Covered Charges'],
precision=4)
viz.color_property='Avg Covered Charges'
viz.color_stops=color_stops
viz.show()
viz.style_url='mapbox://styles/mapbox/dark-v9?optimize=true'
viz.show()
# Generate data breaks and color stops from colorBrewer
color_breaks = mapclassify.Natural_Breaks(df['Avg Covered Charges'].tolist(), k=8, initial=0).bins
color_stops = create_color_stops(color_breaks, colors='Spectral')
# Generate radius breaks from data domain and circle-radius range
radius_breaks = mapclassify.Natural_Breaks(df["Avg Medicare Payments"].tolist(), k=8, initial=0).bins
radius_stops = create_radius_stops(radius_breaks, 1, 10)
# Create the viz
viz2 = GraduatedCircleViz(df_to_geojson(
df,
properties=['Avg Covered Charges', 'Avg Medicare Payments'],
precision=4),
access_token=token,
color_property = "Avg Covered Charges",
color_stops = color_stops,
radius_property = "Avg Medicare Payments",
radius_stops = radius_stops,
center = (-95, 40),
zoom = 3,
below_layer = 'waterway-label')
viz2.show()
#Create a heatmap
heatmap_color_stops = create_color_stops([0.01,0.25,0.5,0.75,1], colors='RdPu')
heatmap_radius_stops = [[0,1], [15, 40]] #increase radius with zoom
color_breaks = mapclassify.Natural_Breaks(df['Avg Medicare Payments'].tolist(), k=8, initial=0).bins
color_stops = create_color_stops(color_breaks, colors='Spectral')
heatmap_weight_stops = create_weight_stops(color_breaks)
#Create a heatmap
viz3 = HeatmapViz(df_to_geojson(
df,
properties=['Avg Medicare Payments'],
precision=4),
access_token=token,
weight_property = "Avg Medicare Payments",
weight_stops = heatmap_weight_stops,
color_stops = heatmap_color_stops,
radius_stops = heatmap_radius_stops,
opacity = 0.9,
center = (-95, 40),
zoom = 3,
below_layer='waterway-label'
)
viz3.show()
#Create a clustered circle map
viz4 = ClusteredCircleViz(df_to_geojson(
df,
properties=['Avg Medicare Payments'],
precision=4),
access_token=token,
color_stops = create_color_stops([1,10,50,100], colors='BrBG'),
radius_stops = [[1,5], [10, 10], [50, 15], [100, 20]],
cluster_maxzoom = 10,
cluster_radius = 30,
opacity = 0.9,
center = (-95, 40),
zoom = 3
)
viz4.show()