vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.32k stars 794 forks source link

Choropleth Map not working with Two Different files for geomap plotting (.geojson for map and .csv for data) #2458

Closed rahulsingh7 closed 3 years ago

rahulsingh7 commented 3 years ago

Transform_lookup() not working when using Two separate files for map and data.

.........Code Snippet..........

data_uk =alt.UrlData("https://raw.githubusercontent.com/keto08/covid-19/master/COVID19-UK/morbidity_sensitivity.csv")
data_geojson = alt.UrlData("https://raw.githubusercontent.com/keto08/covid-19/master/COVID19-UK/uk_corrected.geojson")

geomap = alt.Chart(data_geojson,title = 'Effect of Population Density on Morbidity')\
.mark_geoshape(stroke='black',strokeWidth=0.125)\
.encode(color= 'dPOP:Q').transform_lookup(
    lookup='code',
    from_=alt.LookupData(data_uk, 'code', ['dPOP'])
).project('mercator')\
.properties( 
    width=800,
    height=500)

geomap

This Gives a Blank Map in Altair. visualization (32)

mattijn commented 3 years ago

Since your source is a GeoJSON FeatureCollection you have to set the property to features for format in UrlData. And since the attributes of a geometry are stored within properties within the GeoJSON definition you'll have to lookup using lookup=properties.code

data_uk =alt.UrlData("https://raw.githubusercontent.com/keto08/covid-19/master/COVID19-UK/morbidity_sensitivity.csv")
data_geojson = alt.UrlData(
    url="https://raw.githubusercontent.com/keto08/covid-19/master/COVID19-UK/uk_corrected.geojson", 
    format=alt.DataFormat(property='features')
)

geomap = alt.Chart(data_geojson,title = 'Effect of Population Density on Morbidity')\
.mark_geoshape(stroke='black',strokeWidth=0.125)\
.encode(color= 'dPOP:Q').transform_lookup(
    lookup='properties.code',
    from_=alt.LookupData(data_uk, 'code', ['dPOP'])
).project('mercator')\
.properties( 
    width=500,
    height=500)
image
rahulsingh7 commented 3 years ago

Thanks, @mattijn , that was very helpful,..... This works for me.. Closing the bug