'Altair choropleth map visualization issue
I'm quite the beginner, so it might be something obvious. I tried making a choropleth map, using this guide with Python's Altair package. All code runs fine, except the very last one. Basically the Altair chart titled "choro" that should "fill" the administrative divisons on my map with colours, doesn't do anything. The resulting map if I overlay it on the background, is identical to the background itself, except for a bit different border strokes. I do not need labels, so I left out that one.
This is my code to be precise:
choro = alt.Chart(choro_data).mark_geoshape(
fill='lightgray',
stroke='black'
).encode(
alt.Color('properties.unemp',
type='quantitative',
scale=alt.Scale(scheme='bluegreen'),
title = "Unemployment by county")
).project(
type= 'mercator',
scale= 2000,
center= [20,47],
clipExtent= [[0, 0], [400, 300]]
)
The type of 'unemp' is float, if it is relevant.
What is the problem? Is the guide dated? Do I miss something obvious?
Solution 1:[1]
- have used California geometry and unemployment data
- two issues I found with altair
- skip
fill='lightgray' - projection generates errors whenever passing anything other than
type
- skip
import geopandas as gpd
import pandas as pd
import requests
import altair as alt
# california counties
gdf = gpd.read_file(
"https://raw.githubusercontent.com/codeforgermany/click_that_hood/main/public/data/california-counties.geojson"
)
# california un employment
df = pd.json_normalize(
requests.get("https://data.edd.ca.gov/resource/e6gw-gvii.json").json()
).loc[lambda d: d["area_type"].eq("County") & d["seasonally_adjusted_y_n"].eq("N")]
# make names consistent between geometry and unemployment data
df["name"] = df["area_name"].str.split(" ").apply(lambda l: " ".join(l[0:-1]))
df["unemp"] = pd.to_numeric(df["unemployment_rate"])
# join geometry and unemployment data
gdf = gdf.merge(df, on="name")
geojson = gdf.loc[:, ["name", "unemp", "geometry"]].__geo_interface__
choro_data = alt.Data(values=geojson["features"])
choro = (
alt.Chart(choro_data)
.mark_geoshape(stroke="black")
.encode(
alt.Color(
"properties.unemp",
type="quantitative",
scale=alt.Scale(scheme="bluegreen"),
title="Unemployment by county",
)
)
.project(
type="mercator",
# scale=2000, center=[20, 47], clipExtent=[[0, 0], [400, 300]]
)
)
choro
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rob Raymond |

