我下面有隐藏/显示一行的代码。但是,当我放大时,它似乎不起作用。图例需要以某种方式刷新吗?
fig, ax = plt.subplots(2,1, sharex=True)
ax[0].grid(True)
ax[0].set_title(date)
lines1 = ax[0].plot(x, y)
ax[1].set_xlabel('time')
ax[1].grid(True)
lines2 = ax[1].plot(x2, y2)
leg1 = fig.legend(lines1, header1, 'upper left')
leg2 = fig.legend(lines2, header2, 'lower left')
lined = dict()
for legline, origline in zip(leg1.get_lines(),lines1):
legline.set_picker(5)
lined[legline] = origline
for legline, origline in zip(leg2.get_lines(),lines2):
legline.set_picker(5)
lined[legline] = origline
def onpick(event):
legline = event.artist
origline = lined[legline]
vis = not origline.get_visible()
origline.set_visible(vis)
if vis:
legline.set_alpha(1.0)
else:
legline.set_alpha(0.2)
fig.canvas.draw()
fig.canvas.mpl_connect('pick_event', onpick)
答案 0 :(得分:0)