资源简介
本例代码使用了Python的PyQt5、matplotlib和Dataframe画图,并在图中添加了一条随鼠标移动的虚线,之后经过计算在画图的线上标注出了鼠标在当时x轴停留时的数据,本例只是一个简单的例子,自己可以根据功能修改
代码片段和文件信息
from matplotlib.widgets import MultiCursor
import scipy.spatial as spatial
import numpy as np
def fmt(x y):
return ‘x: {x:0.2f}\ny: {y:0.2f}‘.format(x=x y=y)
class FollowDotCursor(object):
def __init__(self canvas ax df tolerance=5 formatter=fmt offsets=(20 20)):
self.offsets = offsets
self.df = df
self._points = np.column_stack((df[‘Time‘] df[‘A‘]))
self.scale = df[‘Time‘].ptp()
self.scale = df[‘A‘].ptp() / self.scale if self.scale else 1
self.tree = spatial.cKDTree(self.scaled(self._points))
self.formatter = formatter
self.tolerance = tolerance
self.ax = ax
self.fig = ax.figure
self.ax.xaxis.set_label_position(‘top‘)
self.dot = ax.scatter(
[df[‘Time‘].min()] [df[‘A‘].min()] s=130 color=‘green‘ alpha=0.7)
self.annotation = self.setup_annotation()
self.cid = self.fig.canvas.mpl_connect(‘motion_notify_event‘ self)
axs = list()
axs.append(ax)
self.cursor = MultiCursor(canvas axs lw=1 ls=‘--‘)
self.up_points = None
def __call__(self event):
ax = self.ax
inv = ax.transData.inverted()
x y = inv.transform([(event.x event.y)]).ravel()
annotation = self.annotation
x y = self.snap(x y)
annotation.xy = x y
annotation.set_text(self.formatter(x y))
self.dot.set_offsets((x y))
event.canvas.draw()
def scaled(self points):
if len(points) == 2:
time = points[0]
xy = self.df[self.df[“Time“] <= time]
dy = self.df[self.df[“Time“] > time]
if xy.empty or dy.empty:
return self.up_points
d_min = xy[‘Time‘].max()
d_max = dy[‘Time‘].min()
pi = (d_min + d_max) / 2.0
if time <= pi:
aa = self.df[“A“][self.df[“Time“] == d_min]
points = (d_min aa)
else:
aa = self.df[“A“][self.df[“Time“] == d_max]
points = (d_max aa)
self.up_points = points
return points
points = np.asarray(points)
return points * (self.scale 1)
def setup_annotation(self):
“““Draw and hide the annotation box.“““
annotation = self.ax.annotate(
‘‘ xy=(0 0) ha=‘right‘
xytext=self.offsets textcoords=‘offset points‘
va=‘bottom‘
bbox=dict(
boxstyle=‘roundpad=0.5‘ fc=‘yellow‘ alpha=0.75)
arrowprops=dict(
arrowstyle=‘->‘ connectionstyle=‘arc3rad=0‘))
return annotation
def snap(self x y):
“““Return the value in self.tree closest to x y.“““
dist idx = self.tree.query(self.scaled((x y)) k=1 p=1)
try:
return self._points[idx]
except IndexError:
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2217 2018-09-11 13:40 MyAnmateTest.py
文件 3078 2018-09-11 11:53 FollowDotCursor.py
- 上一篇:K-Means算法 python实现
- 下一篇:20180913案例数据
相关资源
- python+pyqt5+百度AI+车牌识别.rar
- python绘制新型冠状病毒疫情地图与疫
- Python3.x+Pyqt5实现界面编程浏览网页
- Python 数据可视化 matplotlib-3.1.1-cp37-c
- Python-用pyqt5和parametrics实现很酷的动画
- matplotlib 安装包
- PyQt5 Python 桌面应用程序源码.zip
- matplotlib-3.2.1-cp37-cp37m-win_amd64.whl
- matplotlib win32 python2.7画图包
- python pyqt5 计时器源代码
- matplotlibwin32
- matplotlib-3.1.2-cp36-cp36m-win_amd64.whl
- matplotlib-2.0.0b2-cp34-cp34m-win32.whl
- matplotlib-1.4.3 windows python2.7 32位及64位
- pyqt5_python_Gui入门教程.docx
- 利用摄像头拍照并保存照片程序pyth
- QT5 Python GUI Programming Cookbook - 2018
- python-matplotlib英文版翻译
- win7,64位,python3.5.2下的安装包:nu
- Python3.4 PyQt5 32位安装版PyQt5-5.5.1-gpl-
- PyQt5 5.3.2 gpl Py3.4 Qt5.3.1 x32.exe
- PyQt5-5.12-5.12.1_a-cp35.cp36.cp37.cp38-none-w
- Serial assistant.rar
-
ba
semap-1.2.1-cp38-cp38m-win_amd64.whl - PyQt5快速开发与实战pdf+源码.zip
- 创建画板,手写体实时在线识别
- PyQt5_Tools-5.7.dev1-py3-none-any.whl
- PyQt5快速开发与实战.zip
- python数据分析之numpy-pandas-matplotlib-常
- matplotlib-3.1.1.tar.gz
评论
共有 条评论