我目前正在使用此代碼在地圖上彈出註釋,當我點擊底圖Matplotlib圖中的一個點時。Python和Matplotlib以及帶鼠標懸停的註釋
dcc = DataCursor(self.figure.gca())
self.figure.canvas.mpl_connect('pick_event',dcc)
plot_handle.set_picker(5)
self.figure.canvas.draw()
class DataCursor(object):
import matplotlib.pyplot as plt
text_template = 'x: %0.2f\ny: %0.2f'
x, y = 0.0, 0.0
xoffset, yoffset = -20 , 20
text_template = 'A: %s\nB: %s\nC: %s'
def __init__(self, ax):
self.ax = ax
self.annotation = ax.annotate(self.text_template,
xy=(self.x, self.y), xytext=(0,0),
textcoords='axes fraction', ha='left', va='bottom', fontsize=10,
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=1),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')
)
self.annotation.set_visible(False)
self.annotation.draggable()
def __call__(self, event):
self.event = event
self.x, self.y = event.mouseevent.xdata, event.mouseevent.ydata
if self.x is not None:
glim = pickle.load(open("ListA.py","rb"))
tlim = pickle.load(open("ListB.py","rb"))
vlim = pickle.load(open("ListC.py","rb"))
a = glim[event.ind[0]] # ['Name'][event.ind[0]]
b = tlim[event.ind[0]]
c = vlim[event.ind[0]]
temp_temp=self.text_template % (a, b, c)
if temp_temp == self.annotation.get_text() and self.annotation.get_visible():
self.annotation.set_visible(False)
event.canvas.draw()
return
self.annotation.xy = self.x, self.y
self.annotation.set_text(self.text_template % (a, b, c))
self.annotation.set_visible(True)
event.canvas.draw()
我想知道的是,如何使用鼠標懸停而不是點擊某個點來顯示註釋?
我看到「motion_notify_event」,但它似乎代碼獲取錯誤時,我將鼠標移動到情節區域周圍。有什麼想法嗎?
我已經看到這兩個鏈接,但我不知道如何以我目前的格式實現它們。我也不知道「pick_event」是一個不可點擊的動作? – mcfly
我接受這個答案,但這不是正確的答案。然而,Root確實鏈接了另一個關聯這個[question]的問題(http://stackoverflow.com/questions/4453143/point-and-line-tooltips-in-matplotlib/4620352#4620352),它給出了懸停和顯示的正確方法註釋。注意:我仍然使用註釋來代替wx.tooltip。它工作得很好! – mcfly