Search code examples
pythonpandasfiguregeopandasmap-projections

Managing projections when plotting in Geopandas


I am using geopandas to draw a map of Italy.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize = (20,30))

region_map.plot(ax=ax, color='white', edgecolor='black')
plt.xlim([6,19])
plt.ylim([36,47.7])
plt.tight_layout()
plt.show()

And this is the results, after properly defining region_map as a piece of 'geometry' GeoSeries .

map of italy

However, I am unable to modify the figure aspect ratio, even varying figsize in plt.subplots. Am I missing something trivial, or is it likely to be a geopandas issue?

Thanks


Solution

  • Your source dataset (region_map) is obviously "encoded" in geographic coordinate system (units: lats and lons). It is safe to assume in your case this is WGS84 (EPSG: 4326). If you want your plot to look more like it does in e.g Google Maps, you will have to reproject its coordinates into one of many projected coordinate systems (units: meters) . You can use globally acceptable WEB MERCATOR (EPSG: 3857).

    Geopandas makes this as easy as possible. You only need to know the basics of how we deal with coordinate projections in computer science and learning most popular CRSes by their EPSG code.

    import matplotlib.pyplot as plt
    
    #If your source does not have a crs assigned to it, do it like this:
    region_map.crs = {"init": "epsg:4326"}
    
    #Now that Geopandas what is the "encoding" of your coordinates, you can perform any coordinate reprojection
    region_map = region_map.to_crs(epsg=3857)
    
    fig, ax = plt.subplots(figsize = (20,30))
    region_map.plot(ax=ax, color='white', edgecolor='black')
    
    #Keep in mind that these limits are not longer referring to the source data!
    # plt.xlim([6,19])
    # plt.ylim([36,47.7])
    plt.tight_layout()
    plt.show()
    

    I highly recommend reading official GeoPandas docs regarding managing projections.