0

I'm trying to create a map of the following GeoJSON: https://github.com/nychealth/coronavirus-data/blob/master/Geography-resources/UHF_resources/UHF42.geo.json

I load it with GeoPandas and can plot it fine with matplotlib:

enter image description here

But when I try to plot it with Altair I get a blue square:

enter image description here

I don't know why it's not working. I've tried plotting other GeoJSONs with Altair and they work fine. I have also checked the geodataframe's crs and it's WGS 84, which is the recommended one for Altair.

Here's my code:

import pandas as pd
import geopandas as gpd

gdf = gpd.read_file('https://raw.githubusercontent.com/nychealth/coronavirus-data/master/Geography-resources/UHF_resources/UHF42.geo.json')

print(gdf.crs)

# Matplotlib plot
gdf.plot()

# Altair plot
alt.Chart(gdf).mark_geoshape()

1 Answers1

0

I'm new to working with maps in Altair, but here's a great answer: from a URL, you need to use alt.Data(url,format) to convert it to data.

Edit: Since you want to use geopandas to make use of it, I used data from the same github to visualize the 7 days data, since the current geopandas doesn't have data to graph. and associated it with 'id'.

import pandas as pd
import geopandas as gpd
import altair as alt

gdf = gpd.read_file('https://raw.githubusercontent.com/nychealth/coronavirus-data/master/Geography-resources/UHF_resources/UHF42.geo.json')
#print(gdf.crs)
data_url = 'https://raw.githubusercontent.com/nychealth/coronavirus-data/master/latest/now-transmission-by-uhf42.csv'

df =pd.read_csv(data_url)
df.columns = ['id', 'neighborhood_name', 'case_rate_7day']

url_geojson = 'https://raw.githubusercontent.com/nychealth/coronavirus-data/master/Geography-resources/UHF_resources/UHF42.geo.json'
data_geojson_remote = alt.Data(url=url_geojson, format=alt.DataFormat(property='features',type='json'))

alt.Chart(data_geojson_remote).mark_geoshape().encode(
    color="case_rate_7day:Q"
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(df, 'id', ['case_rate_7day'])
).project(
    type='identity', reflectY=True
)

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32