<jupyter_start><jupyter_text>Mapboxgl Python Library for location data visualizationBased upon project:https://github.com/mapbox/mapboxgl-jupyterGet API Access token from MapBox:https://account.mapbox.com/auth/signup/?route-to=%22https://account.mapbox.com/access-tokens/%22Add the following environment variable:export MAPBOX_ACCESS_TOKEN="<PASSWORD>Characters  Prerequisites- python 3.7- mapboxgl- pandas<jupyter_code>!pip3 install mapboxgl
!pip3 install pandas
import site
site.main()
# now with refreshed module path...<jupyter_output><empty_output><jupyter_text>Polygon Visualizations using geo coordinates of state borders to colorize bounding area<jupyter_code>import os
import random
import pandas as pd
from mapboxgl.utils import *
from mapboxgl.viz import *

# Must be a public token, starting with `pk`
token = os.getenv('MAPBOX_ACCESS_TOKEN')

# JSON join-data object
data = [{"elevation": x, "weight": random.randint(0,100)} for x in range(0, 21000, 10)]

# GeoJSON data object
polygon_data = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": "06",
      "properties": {
        "name": "California",
        "density": 241.7
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
                        [
                          [-123.233256, 42.006186],
                          [-122.378853, 42.011663],
                          [-121.037003, 41.995232],
                          [-120.001861, 41.995232],
                          [-119.996384, 40.264519],
                          [-120.001861, 38.999346],
                          [-118.71478, 38.101128],
                          [-117.498899, 37.21934],
                          [-116.540435, 36.501861],
                          [-115.85034, 35.970598],
                          [-114.634459, 35.00118],
                          [-114.634459, 34.87521],
                          [-114.470151, 34.710902],
                          [-114.333228, 34.448009],
                          [-114.136058, 34.305608],
                          [-114.256551, 34.174162],
                          [-114.415382, 34.108438],
                          [-114.535874, 33.933176],
                          [-114.497536, 33.697668],
                          [-114.524921, 33.54979],
                          [-114.727567, 33.40739],
                          [-114.661844, 33.034958],
                          [-114.524921, 33.029481],
                          [-114.470151, 32.843265],
                          [-114.524921, 32.755634],
                          [-114.72209, 32.717295],
                          [-116.04751, 32.624187],
                          [-117.126467, 32.536556],
                          [-117.24696, 32.668003],
                          [-117.252437, 32.876127],
                          [-117.329114, 33.122589],
                          [-117.471515, 33.297851],
                          [-117.7837, 33.538836],
                          [-118.183517, 33.763391],
                          [-118.260194, 33.703145],
                          [-118.413548, 33.741483],
                          [-118.391641, 33.840068],
                          [-118.566903, 34.042715],
                          [-118.802411, 33.998899],
                          [-119.218659, 34.146777],
                          [-119.278905, 34.26727],
                          [-119.558229, 34.415147],
                          [-119.875891, 34.40967],
                          [-120.138784, 34.475393],
                          [-120.472878, 34.448009],
                          [-120.64814, 34.579455],
                          [-120.609801, 34.858779],
                          [-120.670048, 34.902595],
                          [-120.631709, 35.099764],
                          [-120.894602, 35.247642],
                          [-120.905556, 35.450289],
                          [-121.004141, 35.461243],
                          [-121.168449, 35.636505],
                          [-121.283465, 35.674843],
                          [-121.332757, 35.784382],
                          [-121.716143, 36.195153],
                          [-121.896882, 36.315645],
                          [-121.935221, 36.638785],
                          [-121.858544, 36.6114],
                          [-121.787344, 36.803093],
                          [-121.929744, 36.978355],
                          [-122.105006, 36.956447],
                          [-122.335038, 37.115279],
                          [-122.417192, 37.241248],
                          [-122.400761, 37.361741],
                          [-122.515777, 37.520572],
                          [-122.515777, 37.783465],
                          [-122.329561, 37.783465],
                          [-122.406238, 38.15042],
                          [-122.488392, 38.112082],
                          [-122.504823, 37.931343],
                          [-122.701993, 37.893004],
                          [-122.937501, 38.029928],
                          [-122.97584, 38.265436],
                          [-123.129194, 38.451652],
                          [-123.331841, 38.566668],
                          [-123.44138, 38.698114],
                          [-123.737134, 38.95553],
                          [-123.687842, 39.032208],
                          [-123.824765, 39.366301],
                          [-123.764519, 39.552517],
                          [-123.85215, 39.831841],
                          [-124.109566, 40.105688],
                          [-124.361506, 40.259042],
                          [-124.410798, 40.439781],
                          [-124.158859, 40.877937],
                          [-124.109566, 41.025814],
                          [-124.158859, 41.14083],
                          [-124.065751, 41.442061],
                          [-124.147905, 41.715908],
                          [-124.257444, 41.781632],
                          [-124.213628, 42.000709],
                          [-123.233256, 42.006186]
                        ]
                      ]
      }
    },
    {
      "type": "Feature",
      "id": "11",
      "properties": {
        "name": "District of Columbia",
        "density": 10065
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
                        [
                          [-77.035264, 38.993869],
                          [-76.909294, 38.895284],
                          [-77.040741, 38.791222],
                          [-77.117418, 38.933623],
                          [-77.035264, 38.993869]
                        ]
                      ]
      }
    },
    {
      "type": "Feature",
      "id": "25",
      "properties": {
        "name": "Massachusetts",
        "density": 840.2
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
                        [
                          [-70.917521, 42.887974],
                          [-70.818936, 42.871543],
                          [-70.780598, 42.696281],
                          [-70.824413, 42.55388],
                          [-70.983245, 42.422434],
                          [-70.988722, 42.269079],
                          [-70.769644, 42.247172],
                          [-70.638197, 42.08834],
                          [-70.660105, 41.962371],
                          [-70.550566, 41.929509],
                          [-70.539613, 41.814493],
                          [-70.260289, 41.715908],
                          [-69.937149, 41.809016],
                          [-70.008349, 41.672093],
                          [-70.484843, 41.5516],
                          [-70.660105, 41.546123],
                          [-70.764167, 41.639231],
                          [-70.928475, 41.611847],
                          [-70.933952, 41.540646],
                          [-71.120168, 41.496831],
                          [-71.196845, 41.67757],
                          [-71.22423, 41.710431],
                          [-71.328292, 41.781632],
                          [-71.383061, 42.01714],
                          [-71.530939, 42.01714],
                          [-71.799309, 42.006186],
                          [-71.799309, 42.022617],
                          [-73.053528, 42.039048],
                          [-73.486206, 42.050002],
                          [-73.508114, 42.08834],
                          [-73.267129, 42.745573],
                          [-72.456542, 42.729142],
                          [-71.29543, 42.696281],
                          [-71.185891, 42.789389],
                          [-70.917521, 42.887974]
                        ]
                      ]
      }
    },
    {
      "type": "Feature",
      "id": "30",
      "properties": {
        "name": "Montana",
        "density": 6.858
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
                        [
                          [-104.047534, 49.000239],
                          [-104.042057, 47.861036],
                          [-104.047534, 45.944106],
                          [-104.042057, 44.996596],
                          [-104.058488, 44.996596],
                          [-105.91517, 45.002073],
                          [-109.080842, 45.002073],
                          [-111.05254, 45.002073],
                          [-111.047063, 44.476286],
                          [-111.227803, 44.580348],
                          [-111.386634, 44.75561],
                          [-111.616665, 44.547487],
                          [-111.819312, 44.509148],
                          [-111.868605, 44.563917],
                          [-112.104113, 44.520102],
                          [-112.241036, 44.569394],
                          [-112.471068, 44.481763],
                          [-112.783254, 44.48724],
                          [-112.887315, 44.394132],
                          [-113.002331, 44.448902],
                          [-113.133778, 44.772041],
                          [-113.341901, 44.782995],
                          [-113.456917, 44.865149],
                          [-113.45144, 45.056842],
                          [-113.571933, 45.128042],
                          [-113.736241, 45.330689],
                          [-113.834826, 45.522382],
                          [-113.807441, 45.604536],
                          [-113.98818, 45.703121],
                          [-114.086765, 45.593582],
                          [-114.333228, 45.456659],
                          [-114.546828, 45.560721],
                          [-114.497536, 45.670259],
                          [-114.568736, 45.774321],
                          [-114.387997, 45.88386],
                          [-114.492059, 46.037214],
                          [-114.464674, 46.272723],
                          [-114.322274, 46.645155],
                          [-114.612552, 46.639678],
                          [-114.623506, 46.705401],
                          [-114.886399, 46.809463],
                          [-114.930214, 46.919002],
                          [-115.302646, 47.187372],
                          [-115.324554, 47.258572],
                          [-115.527201, 47.302388],
                          [-115.718894, 47.42288],
                          [-115.724371, 47.696727],
                          [-116.04751, 47.976051],
                          [-116.04751, 49.000239],
                          [-111.50165, 48.994762],
                          [-109.453274, 49.000239],
                          [-104.047534, 49.000239]
                        ]
                       ]
      }
    },
    {
      "type": "Feature",
      "id": "36",
      "properties": {
        "name": "New York",
        "density": 412.3
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
                        [
                          [-73.343806, 45.013027],
                          [-73.332852, 44.804903],
                          [-73.387622, 44.618687],
                          [-73.294514, 44.437948],
                          [-73.321898, 44.246255],
                          [-73.436914, 44.043608],
                          [-73.349283, 43.769761],
                          [-73.404052, 43.687607],
                          [-73.245221, 43.523299],
                          [-73.278083, 42.833204],
                          [-73.267129, 42.745573],
                          [-73.508114, 42.08834],
                          [-73.486206, 42.050002],
                          [-73.55193, 41.294184],
                          [-73.48073, 41.21203],
                          [-73.727192, 41.102491],
                          [-73.655992, 40.987475],
                          [-73.22879, 40.905321],
                          [-73.141159, 40.965568],
                          [-72.774204, 40.965568],
                          [-72.587988, 40.998429],
                          [-72.28128, 41.157261],
                          [-72.259372, 41.042245],
                          [-72.100541, 40.992952],
                          [-72.467496, 40.845075],
                          [-73.239744, 40.625997],
                          [-73.562884, 40.582182],
                          [-73.776484, 40.593136],
                          [-73.935316, 40.543843],
                          [-74.022947, 40.708151],
                          [-73.902454, 40.998429],
                          [-74.236547, 41.14083],
                          [-74.69661, 41.359907],
                          [-74.740426, 41.431108],
                          [-74.89378, 41.436584],
                          [-75.074519, 41.60637],
                          [-75.052611, 41.754247],
                          [-75.173104, 41.869263],
                          [-75.249781, 41.863786],
                          [-75.35932, 42.000709],
                          [-79.76278, 42.000709],
                          [-79.76278, 42.252649],
                          [-79.76278, 42.269079],
                          [-79.149363, 42.55388],
                          [-79.050778, 42.690804],
                          [-78.853608, 42.783912],
                          [-78.930285, 42.953697],
                          [-79.012439, 42.986559],
                          [-79.072686, 43.260406],
                          [-78.486653, 43.375421],
                          [-77.966344, 43.369944],
                          [-77.75822, 43.34256],
                          [-77.533665, 43.233021],
                          [-77.391265, 43.276836],
                          [-76.958587, 43.271359],
                          [-76.695693, 43.34256],
                          [-76.41637, 43.523299],
                          [-76.235631, 43.528776],
                          [-76.230154, 43.802623],
                          [-76.137046, 43.961454],
                          [-76.3616, 44.070993],
                          [-76.312308, 44.196962],
                          [-75.912491, 44.366748],
                          [-75.764614, 44.514625],
                          [-75.282643, 44.848718],
                          [-74.828057, 45.018503],
                          [-74.148916, 44.991119],
                          [-73.343806, 45.013027]
                        ]
                      ]
      }
    },
    {
      "type": "Feature",
      "id": "49",
      "properties": {
        "name": "Utah",
        "density": 34.3
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
                        [
                          [-112.164359, 41.995232],
                          [-111.047063, 42.000709],
                          [-111.047063, 40.998429],
                          [-109.04798, 40.998429],
                          [-109.053457, 39.125316],
                          [-109.058934, 38.27639],
                          [-109.042503, 38.166851],
                          [-109.042503, 37.000263],
                          [-110.499369, 37.00574],
                          [-114.048427, 37.000263],
                          [-114.04295, 41.995232],
                          [-112.164359, 41.995232]
                        ]
                      ]
      }
    }
  ]
}<jupyter_output><empty_output><jupyter_text>GeoJSON Test polygons Source<jupyter_code># make viz with GeoJSON source
viz = ChoroplethViz(polygon_data,
                    color_property="density",
                    color_stops=[[0.0, "red"], [50.0, "gold"], [1000.0, "blue"]],
                    center=(-95.48, 37.83),
                    zoom=3,
                    access_token=token)
viz.show()<jupyter_output>/usr/local/lib/python3.7/site-packages/IPython/core/display.py:701: UserWarning: Consider using IPython.display.IFrame instead
  warnings.warn("Consider using IPython.display.IFrame instead")<jupyter_text>Add a scale annotation to the heat map of Avg Medicare Payments', 'Avg Covered Charges', 'date' by Geo Coordinates | Property  | Description  | Example ||:--------- |:-------------|:--------|| scale | controls visibility of map scale annotation | True || scale_position | controls the position of the map scale in the map controls pane | 'bottom-right' || scale_background_color | string background color for scale | 'white' || scale_border_color | string border color for scale | 'white' || scale_text_color | string color for legend text | '6e6e6e' |              scale=True,              scale_border_color='eee',              scale_background_color='red',  Create a visualization from example data file<jupyter_code># 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='YlOrRd')

# Create a geojson Feature Collection from the current dataframe
geodata = df_to_geojson(df, 
                        properties=['Avg Medicare Payments', 'Avg Covered Charges', 'date'], 
                        lat='lat', 
                        lon='lon', 
                        precision=3)

# Create the viz from the dataframe
viz = CircleViz(geodata,
                color_property='Avg Medicare Payments',
                color_stops=color_stops,
                radius=2.5,
                stroke_width=0.2,
                center=(-95, 40),
                zoom=2.5,
                scale=True,
                scale_unit_system='imperial',
                below_layer='waterway-label',
                height='300px',
                access_token=token
                )

# Show the viz
viz.show()<jupyter_output><empty_output><jupyter_text>Update scale to match Mapbox Dark-v9 style<jupyter_code># Map settings
viz.style='mapbox://styles/mapbox/dark-v9?optimize=true'
viz.label_color = 'hsl(0, 0%, 70%)'
viz.label_halo_color = 'hsla(0, 0%, 10%, 0.75)'
viz.height = '400px'

# Legend settings
viz.legend_gradient = False
viz.legend_fill = '#343332'
viz.legend_header_fill = '#343332'
viz.legend_text_color = 'hsl(0, 0%, 70%)'
viz.legend_key_borders_on = False
viz.legend_title_halo_color = 'hsla(0, 0%, 10%, 0.75)'

# Scale settings
viz.scale_border_color = 'hsla(0, 0%, 10%, 0.75)'
viz.scale_position = 'top-left'
viz.scale_background_color = '#343332'
viz.scale_text_color = 'hsl(0, 0%, 70%)'

# Render map
viz.show()<jupyter_output><empty_output><jupyter_text>| Property  | Description  | Example ||:--------- |:-------------|:--------|| scale | controls visibility of map scale annotation | True || scale_position | controls the position of the map scale in the map controls pane | 'bottom-right' || scale_background_color | string background color for scale | 'white' || scale_border_color | string border color for scale | 'white' || scale_text_color | string color for legend text | '6e6e6e' |              scale=True,              scale_border_color='eee',              scale_background_color='red',  Create a visualization from example data file<jupyter_code>import pandas as pd
import os
from mapboxgl.utils import *
from mapboxgl.viz import *

# Set Mapbox Acces Token; 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='YlOrRd')

# Create a geojson Feature Collection from the current dataframe
geodata = df_to_geojson(df, 
                        properties=['Avg Medicare Payments', 'Avg Covered Charges', 'date'], 
                        lat='lat', 
                        lon='lon', 
                        precision=3)

# Create the viz from the dataframe
viz = CircleViz(geodata,
                access_token=token, 
                color_property='Avg Medicare Payments',
                color_stops=color_stops,
                radius=2.5,
                stroke_width=0.2,
                center=(-95, 40),
                zoom=2.5,
                scale=True,
                scale_unit_system='imperial',
                below_layer='waterway-label',
                height='300px')

# Show the viz
viz.show()<jupyter_output><empty_output><jupyter_text>Update scale to match Mapbox Dark-v9 style<jupyter_code># Map settings
viz.style='mapbox://styles/mapbox/dark-v9?optimize=true'
viz.label_color = 'hsl(0, 0%, 70%)'
viz.label_halo_color = 'hsla(0, 0%, 10%, 0.75)'
viz.height = '400px'

# Legend settings
viz.legend_gradient = False
viz.legend_fill = '#343332'
viz.legend_header_fill = '#343332'
viz.legend_text_color = 'hsl(0, 0%, 70%)'
viz.legend_key_borders_on = False
viz.legend_title_halo_color = 'hsla(0, 0%, 10%, 0.75)'

# Scale settings
viz.scale_border_color = 'hsla(0, 0%, 10%, 0.75)'
viz.scale_position = 'top-left'
viz.scale_background_color = '#343332'
viz.scale_text_color = 'hsl(0, 0%, 70%)'

# Render map
viz.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13407226778984704, 'fraction_numerical': 0.20243848370649523, 'mean_word_length': 1.0689781691432765, 'pattern_counts': {'":': 52, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '599367', 'license': 'MIT', 'n_tokens_mistral': 9753, 'n_tokens_neox': 6774, 'n_words': 1166, 'path': 'notebooks/mapbox-visualization.ipynb', 'repo_name': 'Stimson-Center/stimson-tutorials', 'size': '22562'}