本文介绍了在图例中单击Data On/Off的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我找到了这个代码: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),然后像有标记而不是线一样再次绘制数据。然后,我们可以继续并将图例线透明度设置为与打开/关闭绘图标记一致。
这篇关于在图例中单击Data On/Off的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!