2017-07-27 47 views
0

所以我找到了這個代碼:https://matplotlib.org/examples/event_handling/legend_picking.html。我試圖讓它適用於數據點而不是線條。所以我想我只是改變'o'中的標記,但這似乎不起作用。 另外我想在動畫中使用它,以便我可以決定是否要遵循數據點。最後,我想要有10個可點擊的圖例條目。點擊圖例中的數據開/關python scatterplot

我試圖做的是隻放一個標記在一號線,2號線和代號:

import numpy as np 
import matplotlib.pyplot as plt 

t = np.arange(0.0, 0.2, 0.1) 
y1 = 2*np.sin(2*np.pi*t) 
y2 = 4*np.sin(2*np.pi*2*t) 

fig, ax = plt.subplots() 
ax.set_title('Click on legend line to toggle line on/off') 
line1, = ax.plot(t, y1, ls='None', marker='o', color='red', label='1 HZ') 
line2, = ax.plot(t, y2, ls='None', marker='o', color='blue', label='2 HZ') 
leg = ax.legend(loc='upper left', fancybox=True, shadow=True) 
leg.get_frame().set_alpha(0.4) 

# we will set up a dict mapping legend line to orig line, and enable 
# picking on the legend line 
lines = [line1, line2] 
lined = dict() 
print(lined) 

for legline, origline in zip(leg.get_lines(), lines): 
    legline.set_picker(5) # 5 pts tolerance 
    lined[legline] = origline 

def onpick(event): 
    # on the pick event, find the orig line corresponding to the 
    # legend proxy line, and toggle the visibility 
    legline = event.artist 
    origline = lined[legline] 
    vis = not origline.get_visible() 
    origline.set_visible(vis) 
    # Change the alpha on the line in the legend so we can see what lines 
    # have been toggled 
    if vis: 
     legline.set_alpha(1.0) 
    else: 
     legline.set_alpha(0.2) 
    fig.canvas.draw() 

fig.canvas.mpl_connect('pick_event', onpick) 

plt.show() 

謝謝看我的問題!

回答

0

不知道,如果你還在尋找一個答案,但我想同樣的事情,這是我調整了您鏈接到代碼:

""" 
Enable picking on the legend to toggle the original line on and off 
""" 
import numpy as np 
import matplotlib.pyplot as plt 

t = np.arange(0.0, 0.2, 0.02) 
y1 = 2*np.sin(2*np.pi*t) 
y2 = 4*np.sin(2*np.pi*2*t) 

fig, ax = plt.subplots() 
ax.set_title('Click on legend line to toggle line on/off') 
line1leg, = ax.plot(0, 0, lw=2, c='r', label='1 HZ') 
line2leg, = ax.plot(0, 0, lw=2, c='b', label='2 HZ') 
line1, = ax.plot(t, y1, 'ro', lw=0, label='_nolegend_') 
line2, = ax.plot(t, y2, 'bo', lw=0, label='_nolegend_') 
leg = ax.legend(fancybox=True, shadow=True) 
leg.get_frame().set_alpha(0.4) 


# we will set up a dict mapping legend line to orig line, and enable 
# picking on the legend line 
lines = [line1, line2] 
lined = dict() 
for legline, origline in zip(leg.get_lines(), lines): 
    legline.set_picker(5) # 5 pts tolerance 
    lined[legline] = origline 


def onpick(event): 
    # on the pick event, find the orig line corresponding to the 
    # legend proxy line, and toggle the visibility 
    legline = event.artist 
    origline = lined[legline] 
    vis = not origline.get_visible() 
    origline.set_visible(vis) 
    # Change the alpha on the line in the legend so we can see what lines 
    # have been toggled 
    #if not vis: 
    # legline.set_alpha(2) 
    # legline.set_marker('^') 
    #else: 
    # legline.set_linewidth(3) 
    # legline.set_marker('<') 
    if vis: 
     legline.set_alpha(1.0) 
    else: 
     legline.set_alpha(0.2) 
    fig.canvas.draw() 

fig.canvas.mpl_connect('pick_event', onpick) 

plt.show() 

它本質上是一個騙子,但我發現如果我將標記添加到圖中,我無法更改圖例中出現的標記。因此,我們將數據作爲一條線繪製,只給出點(0,0),然後再用標記和無線條繪製數據。然後,我們可以繼續並設置圖例線條透明度,以與打開/關閉繪圖標記一致。