2017-07-27 49 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),然后再用标记和无线条绘制数据。然后,我们可以继续并设置图例线条透明度,以与打开/关闭绘图标记一致。