gpt4 book ai didi

python - Matplotlib 演示代码不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 19:25:37 26 4
gpt4 key购买 nike

我正在尝试从 Matplotlib 运行演示代码:legend_picking example .

此代码应该在单击图例时隐藏和显示情节线。

当我单击图例上的行时,事件“pick_event”似乎没有被触发。我对 simple picking example 没有任何问题

"""
# Enable picking on the legend to toggle the legended line on and off
"""
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 = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('Click on legend line to toggle line on/off')
line1, = ax.plot(t, y1, lw=2, color='red', label='1 HZ')
line2, = ax.plot(t, y2, lw=2, 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()
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 visibilit
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()

最佳答案

您正在运行哪个版本的 matplotlib?对我来说效果很好(版本1.1.0)。 sourceforge 网站上有多个示例,这些示例不适用于 1.0 以下的版本。要查找版本号,请使用

import matplotlib
print matplotlib.__version__

关于python - Matplotlib 演示代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8270587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com