[英]Matplotlib demo code not working
我正在嘗試從Matplotlib運行演示代碼: legend_picking示例 。
單擊圖例時,此代碼應該隱藏和顯示繪圖線。
當我單擊圖例上的行時,似乎未觸發事件“ pick_event”。 我對簡單的揀貨示例沒有任何問題
"""
# 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__
確實有效。 只需單擊圖例中的行即可。
該腳本應該通過單擊圖例中的線條來切換圖形的透明度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.