H. Dave
H. Dave

Reputation: 599

How to get x y coordinates from a Matplotlib scatterplot graphic

I have a Widget into my GUI hosting a scatterplot graphic. I managed to include a pick event but am not able to get the x y coordinate by clicking a plot only the print('yahoo') works. My code is based on the following article: see link (section: Simple picking example). How can I get the coordinates of the point (e.g. x=20 and y=50 which is one my five points) by clicking the point on the scatterplot?

Front-end code:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(595, 393)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setMaximumSize(QtCore.QSize(100, 16777215))
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setMinimumSize(QtCore.QSize(0, 200))
        self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.widget.setObjectName("widget")
        self.gridLayout.addWidget(self.widget, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 595, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Back-end code:

import sys
import matplotlib.pyplot as plt
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from front_end import Ui_MainWindow


class Graph_init(FigureCanvas):

    def __init__(self, parent=None):

        fig = Figure()
        fig.patch.set_facecolor("None")
        self.axes = fig.add_subplot(111)
        self.compute_initial_figure()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

        FigureCanvas.mpl_connect(self, 'pick_event', self.onclick)

    def onclick(self, event):
        print('yahoo')
        thisline = self.event.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()


class Graph_populate(Graph_init):

    def compute_initial_figure(self):

        x = [10,20,30,40,50]
        y = [100,50,150,200,75]
        size = [1000,2000,3000,5000,2000]

        self.axes.scatter(x,y,s=size,color='blue', picker=1)
        self.axes.patch.set_facecolor('None')


class GUI(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)
        self.setupUi(self)
        self.sc = Graph_populate(self.widget)
        self.gridLayout.addWidget(self.sc, 0, 1, 1, 1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    prog = GUI()
    prog.showMaximized()
    sys.exit(app.exec_())

Thank you

Upvotes: 0

Views: 2212

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339290

You are plotting a scatterplot. Hence you may have a look at the third part of the pick_event_demo, where a scatterplot is treated.

The idea would be to get the index of the picked datapoint and take the data at that index out to obtain the coordinates.

def onclick(self, event):
    ind = event.ind[0]
    data = event.artist.get_offsets()
    xdata, ydata = data[ind,:]
    print ((xdata, ydata))

Note that those are the coordintes of the data point, not those of the mouseclick (which is how I understand the question).

Upvotes: 2

Related Questions