import sys
import numpy as np
from PyQt5.QtWidgets import QApplication
import pyqtgraph as pg
app = QApplication(sys.argv)
win = pg.GraphicsLayoutWidget(show=True, title="y = exp(x) with Movable Line")
plot = win.addPlot(title="y = exp(x) with Movable Line")
x = np.linspace(-2, 2, 1000)
y = np.exp(x)
plot.plot(x, y, pen='b')
# Create a movable infinite horizontal line with a label that updates based on y-position
h_line = pg.InfiniteLine(pos=1.0, angle=0, movable=True)
plot.addItem(h_line)
h_line.label = pg.InfLineLabel(h_line, text=f"y = {h_line.value():.2f}", position=0.05, color='r', fill=(255, 255, 255, 255))
def update_label():
h_line.label.setText(f"y = {h_line.value():.2f}")
h_line.sigPositionChanged.connect(update_label)
sys.exit(app.exec_())