如何创建颜色和标记的图例?

时间:2017-07-17 09:18:09

标签: python matplotlib legend

我想在图例中显示颜色和标记。颜色意味着一件事而标志意味着另一件事它应该看起来像附加的图像。这是我目前的代码:

x = np.arange(20)
y = np.sin(x)

fig, ax = plt.subplots()
line1 = ax.scatter(x[:10],y[:10],20, c="red", picker=True, marker='*')
line2 = ax.scatter(x[10:20],y[10:20],20, c="red", picker=True, marker='^')

ia = lambda i: plt.annotate("Annotate {}".format(i), (x[i],y[i]), visible=False)
img_annotations = [ia(i) for i in range(len(x))] 

def show_ROI(event):
    for annot, line in zip([img_annotations[:10],img_annotations[10:20]], [line1, line2]):
        if line.contains(event)[0]:
            ...
    fig.canvas.draw_idle()

fig.canvas.mpl_connect('button_press_event', show_ROI)

plt.show()

enter image description here

1 个答案:

答案 0 :(得分:6)

以下是如何使用proxy artists创建具有不同标记和颜色的图例的一般示例。

/** @test */
function a_thread_can_be_deleted()
{
    ...
}

enter image description here