2017-07-17 57 views
2

我想在图例中显示颜色和标记。颜色意味着一件事,标记意味着另一件事它应该看起来像附加的图像。这是当前的代码,我有:如何创建颜色和标记的图例?

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

+0

你的意思是两个独立的传说? – DavidG

+0

@DavidG,它可以是一个图例或两个单独的图例,但标记不是颜色特定的。标记可以有不同的颜色。一种颜色可以有不同的标记。 – matchifang

+0

你能否提供一个预期/期望结果的例子(最好是图片)? – DavidG

回答

4

下面是如何使用proxy artists创建具有不同标志和颜色的传奇一般示例。

import matplotlib.pyplot as plt 
import numpy as np 

data = np.random.rand(8,10) 
data[:,0] = np.arange(len(data)) 

markers=["*","^","o"] 
colors = ["crimson", "purple", "gold"] 


for i in range(data.shape[1]-1): 
    plt.plot(data[:,0], data[:,i+1], marker=markers[i%3], color=colors[i//3], ls="none") 

f = lambda m,c: plt.plot([],[],marker=m, color=c, ls="none")[0] 

handles = [f("s", colors[i]) for i in range(3)] 
handles += [f(markers[i], "k") for i in range(3)] 

labels = colors + ["star", "triangle", "circle"] 

plt.legend(handles, labels, loc=3, framealpha=1) 

plt.show() 

enter image description here

相关问题