Altair Choropleth#

Download this notebook from GitHub (right-click to download).


import altair as alt
from vega_datasets import data
import panel as pn

pn.extension('vega', sizing_mode="stretch_width", template="fast")
pn.state.template.param.update(site="Panel", title="Altair Choropleth Maps")

A simple example demonstrating how to use a reactive function depending on a single widget, to render Altair/Vega plots. In this case the Select widget allows selecting between various quantities that can be plotted on a choropleth map.

altair_logo = 'https://altair-viz.github.io/_static/altair-logo-light.png'
states = alt.topo_feature(data.us_10m.url, 'states')
states['url'] = 'https://raw.githubusercontent.com/vega/vega/master/docs/data/us-10m.json'
source = 'https://raw.githubusercontent.com/vega/vega/master/docs/data/population_engineers_hurricanes.csv'
variable_list = ['population', 'engineers', 'hurricanes']
def get_map(variable):
    return alt.Chart(states).mark_geoshape().encode(
        alt.Color(variable, type='quantitative')
    ).transform_lookup(
        lookup='id',
        from_=alt.LookupData(source, 'id', [variable])
    ).properties(
        width="container",
        height=300,
    ).project(
        type='albersUsa'
    ).repeat(
        row=[variable]
    )
logo = pn.panel(altair_logo, height=150, align="center", sizing_mode="fixed")
centered_logo = pn.Column(pn.layout.HSpacer(), logo, pn.layout.HSpacer()).servable(target="sidebar")
variable = pn.widgets.Select(options=variable_list, name='Variable', width=250).servable(target="sidebar")
info=pn.panel("A simple example demonstrating **how to use a *reactive function* depending on a single widget**, to render Altair plots.").servable()
def get_map_pane(variable):
    return pn.pane.Vega(get_map(variable), sizing_mode="stretch_width", margin=(10,100,10,5))

map_pane = pn.panel(pn.bind(get_map_pane, variable=variable)).servable()

Explore the component in the notebook#

pn.Row(
    pn.Column('# Altair Choropleth Maps', logo, variable, sizing_mode="fixed"),
    map_pane,
)

Serve the App#

You can serve the app via panel serve altair_chropleth.ipynb. Add --autoreload for hot reloading while developing.

This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).