图例拾取 - 启用图例拾取以打开和关闭原始线
Legend picking - Enable picking on the legend to toggle the original line on and off
我尝试重现以下示例,但我失败了。
所以我想要实现的是:我有多个数据帧,这些数据帧应该单独绘制——为此我已经创建了一个 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()
所以基本上我只是复制粘贴它。但它没有按我预期的方式工作。有人有什么推荐吗?
您在第一张和第二张图中使用了一些同名变量。第一个图的事件处理试图访问在创建第二个图时被覆盖的变量,这就是事情失败的地方。
如果您确保使用唯一的变量名,一切正常:
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)
fig1, ax1 = plt.subplots()
ax1.set_title('Click on legend line to toggle line on/off')
Line11, = ax1.plot(t, y1, lw=2, label='1 HZ')
Line12, = ax1.plot(t, y2, lw=2, label='2 HZ')
Leg1 = ax1.legend(loc='upper left', fancybox=True, shadow=True)
Leg1.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
Lines1 = [Line11, Line12]
Lined1 = dict()
for Legline, Origline in zip(Leg1.get_lines(), Lines1):
Legline.set_picker(5) # 5 pts tolerance
Lined1[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 = Lined1[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)
fig1.canvas.draw()
fig1.canvas.mpl_connect('pick_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)
fig2, ax2 = plt.subplots()
ax2.set_title('Click on legend line to toggle line on/off')
line21, = ax2.plot(t, y1, lw=2, label='1 HZ')
line22, = ax2.plot(t, y2, lw=2, label='2 HZ')
leg2 = ax2.legend(loc='upper left', fancybox=True, shadow=True)
leg2.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
lines2 = [line21, line22]
lined2 = dict()
for legline, origline in zip(leg2.get_lines(), lines2):
legline.set_picker(5) # 5 pts tolerance
lined2[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 = lined2[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)
fig2.canvas.draw()
fig2.canvas.mpl_connect('pick_event', onpick2)
plt.show()
我尝试重现以下示例,但我失败了。 所以我想要实现的是:我有多个数据帧,这些数据帧应该单独绘制——为此我已经创建了一个 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()
所以基本上我只是复制粘贴它。但它没有按我预期的方式工作。有人有什么推荐吗?
您在第一张和第二张图中使用了一些同名变量。第一个图的事件处理试图访问在创建第二个图时被覆盖的变量,这就是事情失败的地方。
如果您确保使用唯一的变量名,一切正常:
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)
fig1, ax1 = plt.subplots()
ax1.set_title('Click on legend line to toggle line on/off')
Line11, = ax1.plot(t, y1, lw=2, label='1 HZ')
Line12, = ax1.plot(t, y2, lw=2, label='2 HZ')
Leg1 = ax1.legend(loc='upper left', fancybox=True, shadow=True)
Leg1.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
Lines1 = [Line11, Line12]
Lined1 = dict()
for Legline, Origline in zip(Leg1.get_lines(), Lines1):
Legline.set_picker(5) # 5 pts tolerance
Lined1[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 = Lined1[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)
fig1.canvas.draw()
fig1.canvas.mpl_connect('pick_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)
fig2, ax2 = plt.subplots()
ax2.set_title('Click on legend line to toggle line on/off')
line21, = ax2.plot(t, y1, lw=2, label='1 HZ')
line22, = ax2.plot(t, y2, lw=2, label='2 HZ')
leg2 = ax2.legend(loc='upper left', fancybox=True, shadow=True)
leg2.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
lines2 = [line21, line22]
lined2 = dict()
for legline, origline in zip(leg2.get_lines(), lines2):
legline.set_picker(5) # 5 pts tolerance
lined2[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 = lined2[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)
fig2.canvas.draw()
fig2.canvas.mpl_connect('pick_event', onpick2)
plt.show()