Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Altair not plotting geojson correctly #1612

Closed
joepropellernet opened this issue Jul 16, 2019 · 6 comments
Closed

Altair not plotting geojson correctly #1612

joepropellernet opened this issue Jul 16, 2019 · 6 comments

Comments

@joepropellernet
Copy link

joepropellernet commented Jul 16, 2019

I'm trying to plot a geojson in a notebook and while the geopandas plot looks like this;

image

the Altair output looks like this;

image

As the data is pretty big, I'll not copy it out in full here! The full notebook and data are here: https://github.com/joepropellernet/Plotting_Police_areas and should be able to replicate the issue.

One thought I had is that the geojson alternates between polygons and multipolygons but even filtering out to either one or the other doesn't solve the issue.

@jakevdp
Copy link
Collaborator

jakevdp commented Jul 16, 2019

Altair (via Vega-Lite) supports topojson for map shapes. I'm not very familiar with the various geographic data formats, so I don't know what to recommend when it comes to shape files. You might ask on the Vega-Lite issue tracker whether there is a way to visualize shapefiles in Vega-Lite; that answer will apply to Altair as well.

@mattijn
Copy link
Contributor

mattijn commented Jul 16, 2019

Altair plots by default geographic coordinates in WGS 84 (also known as epsg:4326). Since your coordinates are not coordinates in degrees but in meters and since your data represents England I imagined it is projected in the British National Grid (OSGB 1936, epsg 27700).

Setting the projection to epsg:27700 and reprojecting it to the default epsg:4326 in geopandas before plotting by Altair worked for me:

import altair as alt
import geopandas as gpd

# load raw url in geodataframe
url = 'https://raw.githubusercontent.com/joepropellernet/Plotting_Police_areas/master/Police_areas.geojson'
gdf = gpd.read_file(url)

# set projection and reproject
gdf.crs = {'init' :'epsg:27700'}
gdf = gdf.to_crs({'init': 'epsg:4326'})

# define inline geojson data object
data_geojson = alt.InlineData(values=gdf.to_json(), format=alt.DataFormat(property='features',type='json')) 

# chart object
alt.Chart(data_geojson).mark_geoshape(
).encode(
    color=alt.Color("properties.pfa16nm:N", legend=alt.Legend(columns=2))
)

image

@mattijn
Copy link
Contributor

mattijn commented Jul 16, 2019

Fixed the typos!

@joepropellernet
Copy link
Author

That's amazing Matt and Jake! Thanks so much, I had no idea of the different types of geojson and the limitations of Vega-lite. Would it be possible to put the specific projection that Altair prefers in the documentation?

@mattijn
Copy link
Contributor

mattijn commented Dec 13, 2019

This issue can be closed since the projection part is documented here: https://altair-viz.github.io/user_guide/data.html#projections

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 13, 2019

Thanks!

@jakevdp jakevdp closed this as completed Dec 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants