win = pg.GraphicsWindow()pl = win.addPlot(row=1, col=0)
pl.plot(my_data)
lr = pg.LinearRegionItem(orientation=pg.LinearRegionItem.Horizontal)pl.addItem(lr)
def on_click(ev): if (ev.button() == QtCore.Qt.LeftButton): lo,hi = lr.getRegion() print (lo,hi) ti = pg.TextItem('', anchor=(0,1.1), color=(146, 146, 146), fill='w') pl.addItem(ti) ti.setText(str(round(lo,3))+ ' ' + str(round(hi,3))) ev.accept()
pl.vb.mouseClickEvent = on_click
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
import pyqtgraph as pg
from pyqtgraph.graphicsItems.InfiniteLine import InfLineLabel
app = QtWidgets.QApplication(sys.argv)
my_data = np.random.random(10)
win = pg.GraphicsWindow()
pl = win.addPlot(row=1, col=0)
pl.plot(my_data)
ti = pg.TextItem('', anchor=(0,1.1), color=(146, 146, 146), fill='w')
pl.addItem(ti)
lr = pg.LinearRegionItem(orientation=pg.LinearRegionItem.Horizontal)
pl.addItem(lr)
# Here's another idea:
# Add the value label functionality to the InfiniteLine components of the LinearRegionItem
lr.lines[0].label = InfLineLabel(lr.lines[0], text="{value:0.3f}")
lr.lines[1].label = InfLineLabel(lr.lines[1], text="{value:0.3f}")
def update_regiontext():
lo,hi = lr.getRegion()
ti.setText("{:0.3f} {:0.3f}".format(lo, hi))
ti.setPos(0, lo)
update_regiontext()
# If you really want to only update on click then uncomment code below
# def on_click(ev):
# if (ev.button() == QtCore.Qt.LeftButton):
# update_regiontext()
# ev.accept()
# pl.vb.mouseClickEvent = on_click
# Otherwise, this will update whenever the region is changed
lr.sigRegionChanged.connect(update_regiontext)
win.show()
sys.exit(app.exec_())yticks=list()for i in dataFinal: pl.plot(i + count) yticks.append((count, depths[plotCount])) count +=120 plotCount += 1
ay=pl.getAxis('left')ay.setTicks([yticks])lr.lines[0].sigPositionChanged.connect(lambda: line_changed(lr.lines[0]))
lr.lines[1].sigPositionChanged.connect(lambda: line_changed(lr.lines[1]))
# Pretend this is your list of y labels
yticks = [[(v, "abcdefghijh"[i]) for i, v in enumerate(np.arange(0, 1.1, 0.1))]]
pl.getAxis("left").setTicks(yticks)
def line_changed(line):
ytick_values = np.array(yticks)[0,:,0].astype(np.float64)
ytick_labels = np.array(yticks)[0,:,1]
# Get label of tick closest to line position
line.label.setText(ytick_labels[np.argmin(np.abs(ytick_values - line.getPos()[1]))])
line_changed(lr.lines[0])
line_changed(lr.lines[1])