Étiquettes de choropleth personnalisées dans Geopandas
import geopandas as gp
import matplotlib.pyplot as plt
def update_custom_legend_labels(ax, new_labels):
current_legend = ax.get_legend()
for ix_current_label, txt_current_label in enumerate(current_legend.texts):
for ix_new_label, txt_new_label in new_labels.items():
if ix_current_label == ix_new_label:
# print(f'current: {ix_current_label, txt_current_label}')
# print(f'new: {ix_new_label, txt_new_label}')
txt_current_label.set_text(txt_new_label)
# print(f'update: {txt_current_label}')
world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))
fig, ax = plt.subplots()
world.plot(ax=ax,
column='pop_est',
cmap='OrRd',
scheme='quantiles',
legend=True,
legend_kwds=dict(loc='upper right',
bbox_to_anchor=(1.5, .9),
fontsize='small',
title="Legend",
frameon=False, fmt="{:.0f}")
)
current_labels = [t.get_text() for t in ax.get_legend().get_texts()]
no_labels = len(current_labels)
new_labels = []
for ix, val in enumerate(current_labels):
if ix == 0: # first item
val = '<' + val.split(',')[0]
elif no_labels == ix + 1: # last item
val = '>' + val.split(',')[0]
else: # remaining items
val = val.replace(',', ' -')
new_labels.append((ix,val))
new_labels = dict(new_labels)
update_custom_legend_labels(ax, new_labels)
ax.set_axis_off()
plt.show()
Horrible Hoopoe