在图例python散点图中单击数据开/关

时间:2017-07-27 06:28:33

标签: python legend interactive clickable scatter

所以我找到了这段代码:https://matplotlib.org/examples/event_handling/legend_picking.html。我试图让它适用于数据点而不是行。所以我认为我只是更改了标记,但这似乎不起作用。 此外,我想在动画中使用它,以便我可以决定是否要遵循数据点。最后,我希望有10个可点击的图例条目。

我尝试做的只是在line1和line2中添加一个标记代码:

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()

感谢您查看我的问题!

1 个答案:

答案 0 :(得分:1)

不确定你是否还在寻找答案,但我想要同样的事情,这就是我调整你链接的代码的方式:

"""
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),然后再次绘制数据,就像标记一样,没有线。然后我们可以继续并设置图例线透明度,以便与打开/关闭绘图标记重合。