Using missing_kwds with geopandas changes the shape of the displayed map

Question:

I’m using Geopandas (0.11.1) to plot data on maps. I’m facing an issue with missing_kwds. As some of my values are undefined, I want them to be colored in a specific way. I do that using the missing_kwds option of the plot method.
However, when using it, the shape of the map slightly changes, which is disgraceful when switching quickly from one to the other.

Here is an example.

A map without using missing_kwds :

import geopandas
import matplotlib.pyplot as plt
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.plot()
plt.savefig('world1.png')

A map using missing_kwds :

import geopandas
import matplotlib.pyplot as plt
import numpy as np
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.loc[df.name=="China", 'pop_est'] = np.nan
df.plot(column="pop_est", missing_kwds=dict(color="lightgray"))
plt.savefig('world2.png')

Those are the two resulting maps.

world1.png:

world1.png

world2.png:

world2.png

In case the difference isn’t clear, here is a GIF that illustrates the shape changes.

world.gif

Does anyone have an idea how I could solve this issue?

Asked By: mikael-s

||

Answers:

Add plt.gca().set_aspect(‘equal’) after df.plot().

Answered By: Li Yupeng
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.