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
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