infline = pg.InfiniteLine(pos=1000, movable=True, label='{value:0.2fms}')
# Assume plot data is stored in arrays of xcoords, yvalues
self.xcoords = np.linspace(0.1, 10, 10)
self.yvalues = np.log(self.xcoords)
# Create the infinite line to select an x coordinate, connect to its moved signal
self.crosshairx = pg.InfiniteLine(angle=90, movable=True)
self.crosshairx.sigPositionChanged.connect(self._crosshairx_changed)
# Keep track of index of currently selected x coordinate, so we can avoid doing
# unnecessary updates when the infiniteline changes but the index of the
# closest x/y data point is still the same
self.selected_x_i = np.argmin(np.abs(self.xcoords - self.crosshairx.value()))
# Slot to get y value of point closest to x value of the infinite line
def _crosshairx_changed(self):
new_i = np.argmin(np.abs(self.xcoords - self.crosshairx.value()))
if new_i != self.selected_x_i:
self.selected_x_i = new_i
# Do something interesting
print(self.yvalues[self.selected_x_i]) def cursorLine(self): self.selected_x_i = 0 self.selected_x = str(self.curvesData.get(0)[self.selected_x_i])
# Create the infinite line to select an x coordinate, connect to its moved signal self.crosshairx = pg.InfiniteLine(angle=90, movable=True, label=self.selected_x) self.crosshairx.sigPositionChanged().connect(self._crosshairx_changed)
self.crosshairx.setBounds(bounds=(0,len(self.curvesData.get(0))))
# Keep track of index of currently selected x coordinate, so we can avoid doing # unnecessary updates when the infiniteline changes but the index of the # closest x/y data point is still the same #self.selected_x_i = np.argmin(np.abs(len(self.curvesData.get(0)) - self.crosshairx.value()))
self.plotItem.scene().addItem(self.crosshairx)
def _crosshairx_changed(self): new_i = np.argmin(np.abs(len(self.curvesData.get(0)) - self.crosshairx.value())) if new_i != self.selected_x_i: self.selected_x_i = new_i self.selected_x = str(self.curvesData.get(0)[self.selected_x_i]) print(self.curvesData(0)[self.selected_x_i])self.selected_x_i = np.argmin(np.abs(self.xcoords - self.crosshairx.value()))self.selected_x_i = np.argmin(np.abs(len(self.curvesData.get(0)) - self.crosshairx.value()))self.selected_x_i = np.argmin(np.abs(np.arange(len(self.curvesData.get(0))) - self.crosshairx.value()))self.selected_x_i = int(np.round(self.crosshairx.value()))#!/usr/bin/env python3
from PyQt5 import QtWidgets
import pyqtgraph as pg
import numpy as np
class CurveData():
def __init__(self):
self.data = np.random.rand(100)
def get(self, index):
return self.data
class TestPlot(pg.GraphicsLayoutWidget):
def __init__(self):
super().__init__()
self.curvesData = CurveData()
self.plotItem = self.addPlot()
self.plotItem.plot(self.curvesData.get(0))
self.cursorLine()
def cursorLine(self):
self.selected_x_i = 0
# Create the infinite line to select an x coordinate, connect to its moved signal
self.crosshairx = pg.InfiniteLine(angle=90, movable=True, label="{:0.2f}".format(self.curvesData.get(0)[self.selected_x_i]))
self.crosshairx.sigPositionChanged.connect(self._crosshairx_changed)
self.crosshairx.setBounds(bounds=(0,len(self.curvesData.get(0)) - 1))
# Keep track of index of currently selected x coordinate, so we can avoid doing
# unnecessary updates when the infiniteline changes but the index of the
# closest x/y data point is still the same
self.selected_x_i = int(np.round(self.crosshairx.value()))
self.plotItem.addItem(self.crosshairx)
def _crosshairx_changed(self):
new_i = int(np.round(self.crosshairx.value()))
if new_i != self.selected_x_i:
self.selected_x_i = new_i
self.crosshairx.label.setFormat("{:0.2f}".format(self.curvesData.get(0)[self.selected_x_i]))
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
mainwindow = TestPlot()
mainwindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()