圆形补丁作为matplotlib图例标记

时间:2017-11-15 12:22:47

标签: python matplotlib

我使用以下内容在图像上绘制了一些圆圈:

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)

然后,我可以使用this question的答案之一为其添加图例:

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

产生这个数字:

enter image description here

如何将图例手柄从矩形更改为圆形(类似于在图像上绘制的图形)?

我尝试过使用

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

仍会在图例中生成一个矩形。我也试过

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

仍然会生成一个矩形,但没有填充。期望的结果应该是使用圆圈,颜色和填充图像上绘制的颜色作为图例标记。

我正在使用Python 3.4和matplotlib 2.1.0

0 个答案:

没有答案
相关问题