我希望能夠在散點圖中按點時顯示值。該解決方案在這裏找到:Possible to make labels appear when hovering over a point in matplotlib?python matplotlib - event和mpl_connect
解決方案:
from matplotlib.pyplot import figure, show
import numpy as npy
from numpy.random import rand
# picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
x, y, c, s = rand(4, 100)
def onpick3(event):
ind = event.ind
print 'onpick3 scatter:', ind, npy.take(x, ind), npy.take(y, ind)
fig = figure()
ax1 = fig.add_subplot(111)
col = ax1.scatter(x, y, 100*s, c, picker=True)
#fig.savefig('pscoll.eps')
fig.canvas.mpl_connect('pick_event', onpick3)
秀()
而且它解決了我的問題。但我不明白如何,我一直在搜索,沒有任何運氣。我知道如何繪製matplotlib,所以這不是我缺乏知識的地方。
我不明白的一件事是onpick3(event)
函數。什麼是這個事件參數?因爲函數本身在沒有任何給定參數的情況下被進一步調用:fig.canvas.mpl_connect('pick_event', onpick)
。
很酷,謝謝你的詳細解釋。做得更清楚一點!但是event.ind呢?是否.ind是收到的參數的某個屬性?它代表什麼? – AltoBalto
是'ind'是'event'類的一個屬性。查看更新答案。 – ImportanceOfBeingErnest