新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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()

当前标题:python中matplotlib实现随鼠标滑动自动标注的方法-创新互联
标题网址:http://www.wjwzjz.com/article/dppecj.html
在线咨询
服务热线
服务热线:028-86922220
TOP