我尝试重现以下示例,但是失败了。因此,我要实现的目标是:我有多个数据框,应该单独绘制这些数据框-为此,我已经创建了一个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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句