Python and Matplotlib and Annotations with Mouse Hover(Python 和 Matplotlib 以及鼠标悬停注释)
本文介绍了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 以及鼠标悬停注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:Python 和 Matplotlib 以及鼠标悬停注释
基础教程推荐
猜你喜欢
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
