python中matplotlib实现随鼠标滑动自动标注

python中matplotlib实现随鼠标滑动自动标注


Python+matplotlib进行鼠标交互,实现动态标注,数据可视化显示,鼠标划过时画一条竖线并使用标签来显示当前值。

Python3.6.5,代码示例:

import matplotlib.pyplot as plt
import numpy as np
	
def Show(y):
	#参数为一个list
	
	len_y = len(y)
	x = range(len_y)
	_y = [y[-1]]*len_y
	
	fig = plt.figure(figsize=(960/72,360/72))
	ax1 = fig.add_subplot(1,1,1)
	
	ax1.plot(x, y, color='blue')
	line_x = ax1.plot(x, _y, color='skyblue')[0]
	line_y = ax1.axvline(x=len_y-1, color='skyblue')
	
	ax1.set_title('aaa')
	#标签
	text0 = plt.text(len_y-1,y[-1],str(y[-1]),fontsize = 10)
	
	def scroll(event):
		axtemp=event.inaxes
		x_min, x_max = axtemp.get_xlim()
		fanwei_x = (x_max - x_min) / 10
		if event.button == 'up':
			axtemp.set(xlim=(x_min + fanwei_x, x_max - fanwei_x))
		elif event.button == 'down':
			axtemp.set(xlim=(x_min - fanwei_x, x_max + fanwei_x))
		fig.canvas.draw_idle() 
	#这个函数实时更新图片的显示内容
	def motion(event):
		try:
			temp = y[int(np.round(event.xdata))]
			for i in range(len_y):
				_y[i] = temp
			line_x.set_ydata(_y)
			line_y.set_xdata(event.xdata)
			######
			text0.set_position((event.xdata, temp))
			text0.set_text(str(temp))
			
			fig.canvas.draw_idle() # 绘图动作实时反映在图像上
		except:
			pass

	fig.canvas.mpl_connect('scroll_event', scroll)
	fig.canvas.mpl_connect('motion_notify_event', motion)
	
	plt.show()

效果演示:
在这里插入图片描述
转载请注明出处:https://blog.csdn.net/qq_38778838/article/details/89040722

  • 21
    点赞
  • 127
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
实现Python+matplotlib数据可视化鼠标悬停自动标注功能,可以使用matplotlib的annotate()函数,该函数可以在添加注释文本。 具体实现步骤如下: 1. 导入必要的库: ```python import matplotlib.pyplot as plt from matplotlib.offsetbox import OffsetImage, AnnotationBbox ``` 2. 创建形并绘制数据: ```python fig, ax = plt.subplots() ax.plot(x_data, y_data) ``` 3. 定义鼠标悬停事件的回调函数,该函数将在鼠标悬停时被调用,根据鼠标所在的数据点位置添加注释文本: ```python def on_hover(event): for i in range(len(x_data)): if event.xdata is not None and event.ydata is not None: if x_data[i] - 0.5 <= event.xdata <= x_data[i] + 0.5 and \ y_data[i] - 0.5 <= event.ydata <= y_data[i] + 0.5: img = plt.imread(image_files[i]) imagebox = OffsetImage(img, zoom=0.2) ab = AnnotationBbox(imagebox, (x_data[i], y_data[i])) ax.add_artist(ab) fig.canvas.draw_idle() ``` 4. 将回调函数绑定到形的鼠标悬停事件上: ```python fig.canvas.mpl_connect('motion_notify_event', on_hover) ``` 完整代码: ```python import matplotlib.pyplot as plt from matplotlib.offsetbox import OffsetImage, AnnotationBbox x_data = [1, 2, 3, 4, 5] y_data = [2, 4, 1, 5, 3] image_files = ['image1.png', 'image2.png', 'image3.png', 'image4.png', 'image5.png'] fig, ax = plt.subplots() ax.plot(x_data, y_data) def on_hover(event): for i in range(len(x_data)): if event.xdata is not None and event.ydata is not None: if x_data[i] - 0.5 <= event.xdata <= x_data[i] + 0.5 and \ y_data[i] - 0.5 <= event.ydata <= y_data[i] + 0.5: img = plt.imread(image_files[i]) imagebox = OffsetImage(img, zoom=0.2) ab = AnnotationBbox(imagebox, (x_data[i], y_data[i])) ax.add_artist(ab) fig.canvas.draw_idle() fig.canvas.mpl_connect('motion_notify_event', on_hover) plt.show() ``` 执行该代码后,将会在添加自动标注功能。当鼠标悬停在数据点附近时,会自动在该点添加注释文本,并显示与该数据点相关的片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值