Question

How do I add legend handles in Matplotlib?

I would like to add a legend to my Python plot, with a title and legend handles. My sincere apologies as a complete novice in Python, I got my code from a post. The code below works, but I want to add a legend. All the plots I have googled deal with line plots with several lines.

import geopandas as gpd
import matplotlib.pyplot as plt
from datetime import date
from mpl_toolkits.basemap import Basemap

map_df = gpd.read_file("../Shapefiles/lso_adm_fao_mlgca_2019/lso_admbnda_adm1_FAO_MLGCA_2019.shx")
risks_df=pd.read_csv("../Output/Wndrisks.csv")

merged_df = map_df.merge(risks_df, left_on=["ADM1_EN"], right_on=["District"])
d = {1: "green", 2: "yellow", 3: "orange", 4: "red"}
colors = map_df["ADM1_EN"].map(risks_df.set_index("District")["risk"].map(d))
ax = map_df.plot(color=colors, edgecolor="k", alpha=0.7, legend=True, legend_kwds={"label": "Risk Level", "orientation": "vertical"})

map = Basemap(projection='merc', llcrnrlon=26.5,llcrnrlat=-31.0,urcrnrlon=30.0,urcrnrlat=-28.5, epsg=4269)
map.drawlsmask(land_color='grey',ocean_color='aqua',lakes=True)

legend = plt.legend(handles=[one, two, three, four], title="Risk Levels",
                loc=4, fontsize='small', fancybox=True)

plt.title(f"Strong Wind Risks 01-10Jun24", y=1.04)

plt.tick_params(
axis="both",        # affect both the X and Y
which="both",       # get rid of both major and minor ticks
top=False,          # get rid of ticks top/bottom/left/right
bottom=False,
left=False,
right=False,
labeltop=False,     # get rid of labels top/bottom/left/right
labelbottom=False,
labelleft=False,
labelright=False)

plt.axis("off")          # Get rid of the border around the map
plt.subplots_adjust(right=0.85) # Nudge the country to the left a bit

plt.savefig('wndriskmap.png', dpi=300)
plt.show()

The data is for the form:

"District","risk"
"Berea",3
"Butha-Buthe",4
"Leribe",4
"Mafeteng",4
"Maseru",4
"Mohale's Hoek",4
"Mokhotlong",4
"Qacha's Nek",4
"Quthing",4
"Thaba-Tseka",4

The plot I get is as attachedwindrisk plot

I can attach the shapefile if required. I want the legend to have a title "Risk Level" and the levels 1=no risk, 2=low risk, 3=medium risk and 4=high risk. what I have included in legend = plt.legend(...) does not work.

Assistance will be appreciated.

 3  59  3
1 Jan 1970

Solution

 1

You could map the risks to the labels and make a categorical plot :

from matplotlib.colors import ListedColormap

colors = {1: "green", 2: "yellow", 3: "orange", 4: "red"}  # or a list
labels = {1: "no risk", 2: "low risk", 3: "medium risk", 4: "high risk"}
catego = map_df["risk"].astype(str).str.cat(map_df["risk"].map(labels), sep="- ")

fig, ax = plt.subplots(figsize=(5, 5))

map_df.plot(
    column=catego,
    categorical=True,
    edgecolor="k",
    alpha=0.7,
    cmap=ListedColormap(colors.values()),
    legend=True,
    legend_kwds={
        "title": "Risk Level",
        "shadow": True,
        "loc": "lower right",
    },
    ax=ax,
)
ax.set_axis_off()
ax.set_title("Strong Winds in 10-D Risks 01-10Jun")

NB: I used the risks as a prefix/workaround to preserve the labels' order in the legend.

enter image description here

2024-07-06
Timeless