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

标签 python legend interactive clickable scatter

所以我找到了这段代码:https://matplotlib.org/examples/event_handling/legend_picking.html .我正试图让它适用于数据点而不是线。所以我想我只是改变了'o'中的标记,但这似乎不起作用。 我也想在动画中使用它,这样我就可以决定是否要跟随数据点。最后我想要 10 个可点击的图例条目。

我想做的是在第 1 行和第 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()

感谢您查看我的问题!

最佳答案

不确定您是否仍在寻找这个问题的答案,但我想要同样的东西,这就是我调整您链接到的代码的方式:

"""
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),然后再次绘制数据,使用标记而不是线。然后我们可以继续并设置图例线透明度以与打开/关闭绘图标记一致。

关于python - 单击图例 python 散点图中的数据开/关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45342912/

相关文章:

python - 如何使用 Beautiful Soup 查找所有评论

python - Matplotlib 轴图例在 barh 中仅显示一个标签

python - 算法 Python 用于在 >>> 和 ... 交互式控制台提示之间做出决定?

python - 将多个值拆分为新行

python - 什么会影响超过 64 个字符的字符串的 Python 字符串比较性能?

R 绘图使用 ggplot facet_grid 添加图例

r - 将 abline 添加到图例

ios - 交互式关闭 ViewController

jquery - 使用 jQuery 实现定时动画

python - 剪贴板到 .txt 文件或类似的 Python OSX