pythonaltairvega-lite

How to zoom mark_geoshape to a specific region in Altair?


I'm currently working with a geodataframe for New York City. However, I want my chart to only show a specific part of the city. Is there a way to set the latitude and longitude max and min in a mark_geoshape?

I would like my chart to show only from latitude 40.64 tot 40.84, and longitude -74.01 to -73.9.

Here's my current code:

ntaMap = alt.Chart(ntaData).mark_geoshape(
    fill='whitesmoke',
    stroke='gray',
    strokeWidth=0.5
).encode(
    tooltip=['NTAName','BoroName']
).properties(
    width=500,
    height=500,
    title='Neighborhood Tabulation Areas'
).configure_view(
    strokeWidth=0
)

ntaMap

Thanks!


Solution

  • If you are using a geopandas dataframe the easiest might be to filter your data to only contain the regions of interest. Otherwise, you can use scale to zoom and translate to pan:

    import altair as alt
    from vega_datasets import data
    
    
    world = data.world_110m.url
    
    alt.Chart(alt.topo_feature(world, 'countries')).mark_geoshape(
        fill='#2a1d0c', stroke='#706545', strokeWidth=0.5
    ).project(
        type='mercator', scale=400, translate=[100, 550])
    

    enter image description here