一、效果展示
开门见山,上效果:
窗口共有三个部分
- 图2为所有数据画出的折线图
- 图1为图2的蓝色矩形区域处的放大显示
- 右上角的图例可以根据鼠标在图1中的移动,实时显示鼠标所在x轴处的折线y值
二、资料参考
- pyqtgraph的examples的Crosshair / Mouse interaction
安装好后pyqtgraph包之后,上面的窗口在命令行中输入python -m pyqtgraph.examples
即可。
三、实例详解
详细解释见代码注释:
# # **********************************************************************
# CrossHair,借鉴显示鼠标所在x值处的线的y值
"""
Demonstrates some customized mouse interaction by drawing a crosshair that follows
the mouse.
"""
# import initExample ## Add path to library (just for examples; you do not need this)
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.Point import Point
#generate layout
app = QtGui.QApplication([]) # 初始化app
win = pg.GraphicsLayoutWidget(show=True) # 设置widget,并属性show为True,即显示
win.setWindowTitle('pyqtgraph example: crosshair') # 设置窗口名称
label = pg.LabelItem(justify='right') # 设置label用于跟随鼠标显示横纵坐标有效值
win.addItem(label) # 定义了label之后,要addItem之后才会真正加进去
p1 = win.addPlot(row=1, col=0) # p1为p2的区域放大,并显示鼠标所在位置值
p2 = win.addPlot(row=2, col=0) # p2为全部数据画出的折线致密的原图
# 定义区域
region = pg.LinearRegionItem()
region.setZValue(10) # 不过这一句话好像没什么作用
# Add the LinearRegionItem to the ViewBox, but tell the ViewBox to exclude this
# item when doing auto-range calculations.
p2.addItem(region, ignoreBounds=True)
#pg.dbg()
p1.setAutoVisible(y=True)
#create numpy arrays
#make the numbers large to show that the xrange shows data from 10000 to all the way 0
data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
data2 = 15000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
# 画图
p1.plot(data1, pen="r")
p1.plot(data2, pen="g")
p2.plot(data1,<