Matplotlib 图例拾取-启用对图例的拾取以打开和关闭原始行

Matplotlib 图例拾取-启用对图例的拾取以打开和关闭原始行,matplotlib,toggle,legend,Matplotlib,Toggle,Legend,我试图重现下面的例子,但我失败了。 所以我想要实现的是:我有多个数据帧,这些数据帧应该单独绘制——为此我已经创建了一个UI。我想点击图例中的项目来打开或关闭该行。为了实现这一点,我使用了以下示例 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.subp

我试图重现下面的例子,但我失败了。 所以我想要实现的是:我有多个数据帧,这些数据帧应该单独绘制——为此我已经创建了一个UI。我想点击图例中的项目来打开或关闭该行。为了实现这一点,我使用了以下示例

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, lw=2, label='1 HZ')
line2, = ax.plot(t, y2, lw=2, 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()
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()
如果复制粘贴这有两个数字(我这样做是为了检查我的错误-因为我有一个错误产生多个绘图),它会给我两个数字,但我不能点击两个数字的图例,只有最后一个,这里是我的代码

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, lw=2, label='1 HZ')
Line2, = ax.plot(t, y2, lw=2, 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()
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()

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, lw=2, label='1 HZ')
line2, = ax.plot(t, y2, lw=2, 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()
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(5)  # 5 pts tolerance
    lined[legline] = origline


def onpick2(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', onpick2)

plt.show()

所以基本上我只是复制粘贴它。但它没有按我预期的方式工作。有人推荐吗?

您使用的一些变量在第一个和第二个图中具有相同的名称。第一个图形的事件处理尝试访问在创建第二个图形时被覆盖的变量,这就是失败的地方

如果确保使用唯一的变量名,则一切正常:

将numpy导入为np
将matplotlib.pyplot作为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)
图1,ax1=plt.subplot()
ax1.设置标题('单击图例行以打开/关闭行')
第11行,=ax1.绘图(t,y1,lw=2,label='1 HZ')
第12行,=ax1.绘图(t,y2,lw=2,label='2 HZ')
Leg1=ax1.legend(loc='左上角',fancybox=True,shadow=True)
Leg1.get_frame().set_alpha(0.4)
#我们将设置一个dict映射图例行到原始行,并启用
#在图例线上拾取
Lines1=[Line11,Line12]
Lined1=dict()
对于Legline,拉链中的Origline(Leg1.get_lines(),Lines1):
腿线。设置选择器(5)#5点公差
Lined1[Legline]=Origline
def onpick(事件):
#在拾取事件中,找到与
#图例代理线,并切换可见性
Legline=Event.artist
Origline=Lined1[Legline]
vis=非原始线。使_可见()
Origline.set_可见(可见)
#更改图例中线条上的alpha,以便我们可以看到哪些线条
#已切换
如果是vis:
Legline.set_alpha(1.0)
其他:
Legline.set_alpha(0.2)
图1.canvas.draw()
图1.canvas.mpl\u connect('pick\u event',onpick)
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)
图2,ax2=plt.子批次()
ax2.设置标题('单击图例行以打开/关闭行')
第21行,=ax2.绘图(t,y1,lw=2,label='1 HZ')
第22行,=ax2.绘图(t,y2,lw=2,label='2 HZ')
leg2=ax2.legend(loc='左上角',fancybox=True,shadow=True)
leg2.get_frame().set_alpha(0.4)
#我们将设置一个dict映射图例行到原始行,并启用
#在图例线上拾取
lines2=[line21,line22]
lined2=dict()
对于legline,拉链中的origline(leg2.get_lines(),lines2):
腿线。设置选择器(5)#5点公差
lined2[legline]=origline
def onpick2(事件):
#在拾取事件中,找到与
#图例代理线,并切换可见性
legline=event.artist
origline=lined2[legline]
vis=非原始线。使_可见()
origline.set_可见(可见)
#更改图例中线条上的alpha,以便我们可以看到哪些线条
#已切换
如果是vis:
legline.set_alpha(1.0)
其他:
legline.set_alpha(0.2)
图2.canvas.draw()
图2.canvas.mpl\u connect(“pick\u事件”,onpick2)
plt.show()

非常感谢。我改变了变量。只是没有意识到我忘了换一些。谢谢