Altair/Vega map not showing. Chrome fails to load SourceMap

Viewed 107

I am trying to place some dots on the map of Europe.

  import altair as alt
  import pandas as pd

  mydf = pd.DataFrame({
    'name': ['Constanta', 'Turin', 'Madrid', 'Copenhagen', 'Berlin'],
    'lat': [45.1598, 45.0703, 40.4168, 55.6761, 52.5200],
    'long': [28.6348, 7.6869, -3.7038, 12.5683, 13.40]
    })

    alt.Chart(mydf).mark_circle().encode(
        longitude='long',
        latitude='lat',
        size=alt.value(50),
        tooltip='name'
    ).project(
        type= 'mercator',
        scale= 350,                          
        center= [20,50],                     
        clipExtent= [[0, 0], [400, 300]],    
    ).properties(
        width=500,
        height=400
    )

The dots appear in the right place, but the map does not show up, it's just a blank plot. In Chrome (Jupyter Notebook) I get the following error:

DevTools failed to load SourceMap: Could not load content for https://cdn.jsdelivr.net/npm/vega.min.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

I tried switching browsers, embedding in Streamlit, updating my libraries.

1 Answers

Altair does not automatically display map backgrounds; you have to do that manually in a layer, something like this:

import altair as alt
from vega_datasets import data
import pandas as pd

countries = alt.topo_feature(data.world_110m.url, 'countries')

mydf = pd.DataFrame({
    'name': ['Constanta', 'Turin', 'Madrid', 'Copenhagen', 'Berlin'],
    'lat': [45.1598, 45.0703, 40.4168, 55.6761, 52.5200],
    'long': [28.6348, 7.6869, -3.7038, 12.5683, 13.40]
})

points = alt.Chart(mydf).mark_circle().encode(
    longitude='long',
    latitude='lat',
    size=alt.value(50),
    tooltip='name'
).project(
    type= 'mercator',
    scale= 350,                          
    center= [20,50],                     
    clipExtent= [[0, 0], [400, 300]],    
).properties(
    width=500,
    height=400
)

background = alt.Chart(countries).mark_geoshape(
    fill='#CCCCCC',
    stroke='white'
).project(
    type= 'mercator',
    scale= 350,                          # Magnify
    center= [20,50],                     # [lon, lat]
    clipExtent= [[0, 0], [400, 300]],    # [[left, top], [right, bottom]]
).properties(
    width=400, height=300
)

background + points

enter image description here

Side note: the "sourceMap" thing was a red herring: that's a javascript concept, not in reference to a geographic map.

Related