本文介紹了Python 和 Matplotlib 以及鼠標懸停注釋的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
當我單擊 Basemap Matplotlib Plot 中的一個點時,我目前正在使用此代碼在地圖上彈出注釋.
I am currently employing this code to have pop up annotatations on a map when i click on a point in a Basemap Matplotlib Plot.
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
y: %0.2f'
x, y = 0.0, 0.0
xoffset, yoffset = -20 , 20
text_template = 'A: %s
B: %s
C: %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()
我想知道的是,如何使用鼠標懸停而不是單擊某個點來顯示注釋?
What I am wondering, is how to show the annotations using mouse hover rather than clicking on a point?
我見過motion_notify_event",但是當我在繪圖區域周圍移動鼠標時,代碼似乎出錯了.有什么想法嗎?
I have seen "motion_notify_event" but it seems the code gets errors when i move the mouse around the plot area. Any Thoughts?
推薦答案
看看this question 和 demo:
from matplotlib.pyplot import figure, show
import numpy as npy
from numpy.random import rand
if 1: # 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)
show()
這篇關于Python 和 Matplotlib 以及鼠標懸停注釋的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!