0

I have the following code:

import geopandas as gpd
import matplotlib.pyplot as plt

states_gdf = gpd.read_file('https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_5m.zip')
CO_gdf = states_gdf[states_gdf['STUSPS'] == 'CO']

fig, ax = plt.subplots()
CO_gdf.plot(ax = ax, column = 'STUSPS')
CO_gdf.boundary.to_frame().plot(ax = ax, color = 'k', label = 'CO Boundary')

I want to have one legend, that labels both the colored-in region and the boundary.

If I try the following code:

fig, ax = plt.subplots()
CO_gdf.plot(ax = ax, column = 'STUSPS', legend = True)
CO_gdf.boundary.to_frame().plot(ax = ax, color = 'k', label = 'CO Boundary')
ax.legend()

The first legend gets deleted when the second one is plotted.

I've also tried collecting the handles and labels manually with ax.get_legend_handles_labels(), but for some reason the first plot line CO_gdf.plot(...) doesn't seem to create any handles.

hm8
  • 1,381
  • 3
  • 21
  • 41

0 Answers0