2017-07-17 56 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

相關問題