import os
from mapboxgl.viz import *
from mapboxgl.utils import *
import pandas as pd
# Must be a public token, starting with `pk`
token = os.getenv('MAPBOX_ACCESS_TOKEN')
# Load data from sample csv
data_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/data/points.csv'
df = pd.read_csv(data_url).round(3)
# Generate data breaks using numpy quantiles and color stops from colorBrewer
measure = 'Avg Medicare Payments'
color_breaks = [round(df[measure].quantile(q=x*0.1), 2) for x in range(1,9)]
color_stops = create_color_stops(color_breaks, colors='YlGnBu')
data = json.loads(df.to_json(orient='records'))
v = CircleViz(data,
access_token=token,
vector_url='mapbox://rsbaumann.2pgmr66a',
vector_layer_name='healthcare-points-2yaw54',
vector_join_property='Provider Id',
data_join_property='Provider Id',
color_property=measure,
color_stops=color_stops,
radius=2.5,
stroke_color='black',
stroke_width=0.2,
center=(-95, 40),
zoom=3,
below_layer='waterway-label',
legend_text_numeric_precision=0)
v.show()
# Generate data breaks and color stops from colorBrewer
measure_color = 'Avg Covered Charges'
color_breaks = [round(df[measure_color].quantile(q=x*0.1), 2) for x in range(2, 10)]
color_stops = create_color_stops(color_breaks, colors='Blues')
# Generate radius breaks from data domain and circle-radius range
measure_radius = 'Avg Medicare Payments'
radius_breaks = [round(df[measure_radius].quantile(q=x*0.1), 2) for x in range(2,10)]
radius_stops = create_radius_stops(radius_breaks, 0.5, 10)
# Create the viz
viz2 = GraduatedCircleViz([],
access_token=token,
vector_url='mapbox://rsbaumann.2pgmr66a',
vector_layer_name='healthcare-points-2yaw54',
disable_data_join=True,
color_property='Avg Covered Charges',
color_stops=color_stops,
radius_property="Avg Medicare Payments",
radius_stops=radius_stops,
stroke_color='black',
stroke_width=0.5,
center=(-95, 40),
zoom=3,
opacity=0.75,
legend_text_numeric_precision=0,
below_layer='waterway-label')
viz2.show()
measure = 'Avg Medicare Payments'
heatmap_color_stops = create_color_stops([0.01, 0.25, 0.5, 0.75, 1], colors='RdPu')
heatmap_radius_stops = [[0, 3], [14, 100]]
color_breaks = [round(df[measure].quantile(q=x*0.1), 2) for x in range(2,10)]
color_stops = create_color_stops(color_breaks, colors='Spectral')
heatmap_weight_stops = create_weight_stops(color_breaks)
viz = HeatmapViz([],
disable_data_join=True,
access_token=token,
vector_url='mapbox://rsbaumann.2pgmr66a',
vector_layer_name='healthcare-points-2yaw54',
weight_property='Avg Medicare Payments',
weight_stops=heatmap_weight_stops,
color_stops=heatmap_color_stops,
radius_stops=heatmap_radius_stops,
opacity=0.8,
center=(-95, 40),
zoom=3,
below_layer='waterway-label')
viz.show()