I've got a notebook (github link) where I'm using geopandas to draw maps with various countries colored. Depending on the order of the plots, sometimes it doesn't respect the figsize() I specify. I'm seeing this behavior repeatably in jupyter running locally in Ubuntu 20.04 and Firefox, and both Binder and Colab running in Chromium.
Can somebody help me understand what's going on? Is this a bug or am I controlling geopandas/matplotlib wrong?
import matplotlib.pyplot as plt
import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
sixc = world[ world['continent'] != 'Antarctica' ]
asia = world[ world['continent'] == 'Asia' ]
noam = world[ world['continent'] == 'North America']
swed = world[ world['iso_a3'] == 'SWE' ]
# This works, makes a 2x1 landscapey aspect
axes = sixc.plot(figsize=(8,4), color='lightgrey')
asia.plot(ax=axes, color='green')
noam.plot(ax=axes, color='purple')
# Plotting swed at the end breaks figsize, makes it squareish
axes = sixc.plot(figsize=(8,4), color='lightgrey')
asia.plot(ax=axes, color='green')
noam.plot(ax=axes, color='purple')
swed.plot(ax=axes, color='yellow')
# Plotting swed in the middle makes it ok again
axes = sixc.plot(figsize=(8,4), color='lightgrey')
asia.plot(ax=axes, color='green')
swed.plot(ax=axes, color='yellow')
noam.plot(ax=axes, color='purple')
(Also, for some (but not all!) of my students, that broken plot also has no lightgray background countries. Is that likely a result/side-effect of whatever is causing the aspect problem?)
With geopandas
v 0.8.1 you are using, the default
aspect ratio of the plot commands you use is auto
. Thus, the output plots you obtained will have unpredictable value of aspect ratio. Try
print(axes.get_aspect())
for each plot. In previous version of geopandas, one will get equal
as output, and the plots are correct. But in your case, you will get values that do not represent equal aspect.
For a simple fix of your problem, you can add this line of code:
axes.set_aspect('equal')
after the last plot statement.