Clicking on plot - possible inconsistent behavior

54 views
Skip to first unread message

wot

unread,
Jun 19, 2017, 9:46:40 PM6/19/17
to pyqtgraph
I want to be able to select a line on a line graph. I was using plot but it doesn't appear to work so I tried PlotCurveItem, which does work. I guess I am missing something but I was expecting plot to work like PlotCurveItem for the line graph. Plotting points works as expected using plot.

import pyqtgraph as pg

from PyQt4 import QtGui

import numpy as np

import sys


 

def main():

   
def clicked1(curve, points):


        print(curve)


        print(points)


   
def clicked2(points):

       
print(points)

         

    app
= QtGui.QApplication(sys.argv)

    widg
= QtGui.QWidget()

    widg
.move(100, 100)

     

    pgWidg
= pg.GraphicsLayoutWidget()

    pgWidg
.resize(750, 250)  

 

    graph
= pgWidg.addPlot(row=1, col=1)

    curve1
= graph.plot(y=np.sin(np.linspace(0, 20, 1000)), symbol='o', clickable=True)

    curve2
= graph.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='r', clickable=True)

    curve3
= pg.PlotCurveItem(y=np.sin(np.linspace(2, 22, 1000)), pen='b', clickable=True)

    graph
.addItem(curve3)

     

    curve1
.sigPointsClicked.connect(clicked1)

    curve2
.sigPointsClicked.connect(clicked1)

    curve2
.sigClicked.connect(clicked2)

    curve3
.sigClicked.connect(clicked2)

     

    grid
= QtGui.QGridLayout()

    grid
.addWidget(pgWidg, 0,0)          

    widg
.setLayout(grid)

    widg
.show()

    sys
.exit(app.exec_())


 

if __name__ == '__main__':

    main
()


Message has been deleted

Patrick Kreitzberg

unread,
Jun 20, 2017, 2:50:02 PM6/20/17
to pyqtgraph
pgWidg.addPlot creates a plotItem called graph in this case.   
graph.plot creates and returns a PlotDataItem. 
The PlotDataItem creates a PlotCurveItem you can get by calling curve2.curve.

So your first two curves have created PlotCurveItems but you are not calling them directly.

To make curve2 act like curve3 do:

curve2.curve.setClickable(True) #can set width=5  so that you can click within  5 pixels to emit signal
curve2.curve.sigClicked.connect(clicked2)

wot

unread,
Jun 20, 2017, 4:04:31 PM6/20/17
to pyqtgraph
Thanks, that moved the ball forward. 
If you set the curve clickable,

curve2.curve.setClickable(True)

then you can access both the PlotDataItem and the PlotCurveItem.
Reply all
Reply to author
Forward
0 new messages