Combine two Pyplot patches for legend

The solution is borrowed from the comment by CrazyArm, found here: Matplotlib, legend with multiple different markers with one label. Apparently you can make a list of handles and assign only one label and it magically combines the two handles/artists.

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,1,11)
y = np.linspace(0,1,11)

p1, = plt.plot(x, y, c="r")  # notice the comma!
plt.fill_between(x, y-0.2, y+0.2, color="r", alpha=0.5)
p2 = mpatches.Patch(color="r", alpha=0.5, linewidth=0)

plt.legend(((p1,p2),), ('Entry',))

Figure

Leave a Comment