Search code examples
pythongeopandas

How to include two legend boxes in a map?


I have a database with geometry for Sao Paulo city and the criminal activity by district. Here it is my table's head. Also, I have 2 geopoints for assets that I want to highlight defined as asset.

For plotting, I have the following code but I do not want to have the black color in the legend, but the marker that I am using (x)

import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from matplotlib.path import Path
import matplotlib.lines as mlines

patches = []
for category, color in color_list.items():
patch = Patch(color=color, label=category)
patches.append(patch)

# creating legend
ax = sao.plot(color=sao["Colors"])

# creating title
ax.set_title('Risk Areas - São Paulo, Brazil', fontweight='bold')

# removing axes
ax.axis('off')

# assets
asset.plot(color='black', markersize = 110, marker= "$x$", ax=ax, label = "assets")

fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
plt.legend(handles=patches + [Patch(color='black', label="assets")], loc='lower right', title = 'Crime Rate Level for Districts')
plt.show()

Can I have the colors for crime rate and a symbol x in the legend? Should I create 2 legend boxes? Do something different?! Open to suggestions

That's what I got so far


Solution

  • You could use matplotlib.lines.Line2D instead of matplotlib.patches.Patch It would look something like this

    from matplotlib.lines import Line2D
    plt.legend(handles=patches + [Line2D([],[], marker='x', linestyle='', color='black', label="assets")], loc='lower right', title = 'Crime Rate Level for Districts')
    

    You can also adjust the size of the marker is you prefer.