5

I am drawing some circles onto an image using the following:

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

image = np.zeros((20,20))

x_coords = [3,12]
y_coords = [3,12]

fig, ax = plt.subplots()
ax.imshow(image)

for a,b in zip(x_coords,y_coords):
    circle = plt.Circle((a, b), 2, color="yellow", fill=False, linewidth=2)
    ax.add_artist(circle)

I can then add a legend onto this using one of the answers to this question:

patches = [mpatches.Patch(color="yellow", label="Test")]
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2)

Which produces this figure:

enter image description here

How do I change the legend handles from a rectangle to a circle (similar to what is drawn onto the image)?

I have tried using

patches = [mpatches.Circle((0,0), 2, color="yellow", label="Test")]
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2)

which still produces a rectangle in the legend. I have also tried

plt.legend(handles=[circle], labels=["Test"], bbox_to_anchor=(1.05, 1), loc=2)

Which still produces a rectangle, but with no fill. The desired result should be to have a circle, the same colour and fill as what is drawn on the image, as the legend marker.

I am using Python 3.4 and matplotlib 2.1.0

DavidG
  • 24,279
  • 14
  • 89
  • 82

0 Answers0