Skip to content
Advertisement

How to restrict a geopandas plot by coordinates?

I created a simple geopandas map using a shape file from OpenStreetMap (link).

JavaScript

Out:

Map of Berlin

How can I restrict the plot to only a certain portion of the image, essentially zooming in on a certain location. Can I do this by specifying the extent of the portion of the map which I’d like to plot in latitude and longitude?

Advertisement

Answer

The .plot() method of geopandas.GeoDataFrame returns a Matplotlib Axes object. In other words, you can simply assign the returned map to an Axes object and then use set_xlim and set_ylim to control the bound values.

For example, let’s use a sample dataset from geopandas

JavaScript

The default map returned looks like the following:

enter image description here

You can use set_xlim and set_ylim to change the bounds.

JavaScript

This will produce the following map

enter image description here


If you want to dynamically change the bounds, each geometry object in GeoDataFrame has a total_bounds attribute. Assuming you want to concentrate on Australia,

JavaScript

This will get you

enter image description here

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement