8

I am making choropleth maps with geopandas. I want to draw maps with two layers of borders: thinner ones for national states (geopandas default), and thicker ones for various economic communities. Is this doable in geopandas?

Here is an example:

import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

africa = world.query('continent == "Africa"')

EAC = ["KEN", "RWA", "TZA", "UGA", "BDI"]

africa["EAC"] = np.where(np.isin(africa["iso_a3"], EAC), 1, 0)

africa.plot(column="pop_est")
plt.show()

I have created a dummy variable for countries belonging to group EAC. I would like to draw a thicker border surrounding the countries in this group, while leaving the national borders in as well.

enter image description here

EDIT:

I still don't know how to make this work in subplots. Here is an example:

axs = ["ax1", "ax2"]
vars = ["pop_est", "gdp_md_est"]
fig, axs = plt.subplots(ncols=len(axs),
                        figsize=(10, 10),
                        sharex=True,
                        sharey=True,
                        constrained_layout=True)

for ax, var in zip(axs, vars):

    africa.plot(ax=ax,
                column=var,
                edgecolor="black",
                missing_kwds={
                    "color": "lightgrey",
                    "hatch": "///"
                })

    ax.set_title(var)

plt.show()

enter image description here

I wasn't able to apply Martin's solution directly.

Antti
  • 1,263
  • 2
  • 16
  • 28

1 Answers1

8

Specify the background plot as an axis and use it within the second plot, plotting only EAC countries. To have only outlines, you need facecolor='none'.

ax = africa.plot(column="pop_est")

africa.loc[africa['EAC'] == 1].plot(ax=ax, facecolor='none', edgecolor='red', linewidth=2)

enter image description here

If you want a boundary only around those countries, you have to dissolve geometries before.

africa.loc[africa['EAC'] == 1].dissolve('EAC').plot(ax=ax, facecolor='none', edgecolor='red', linewidth=2)

enter image description here

martinfleis
  • 7,124
  • 2
  • 22
  • 30