I'm trying to add some functionality to a PyQtGraph-based app. I'd like to click on a curve (trace) and add a data label with the x- and y-values of the nearest data point.
Minimal code sample (Python 3.x, PyQt5):
import sys
import pyqtgraph as pg
from PyQt5.QtCore import *
from PyQt5.QtGui import *
def sig_curve_clicked(evt):
print("Curve clicked: ", evt.pos())
x_data = [1, 2, 3, 4, 5]
y_data = [7, 2, 4, 10, 8]
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
win = pg.GraphicsWindow()
p1 = win.addPlot(title="Plot1")
p1.setRange(yRange=[0, 15])
curve1 = p1.plot(x_data, y_data)
curve1.curve.setClickable(True)
curve1.curve.sigClicked.connect(sig_curve_clicked)
## Start Qt event loop
app.exec_()
When I run this and click on the curve, the pos returns (0, 0).
Curve clicked: Point(0.000000, 0.000000)
How can I get the x-y coordinates of the nearest data point on the clicked trace?