55895


pyqt5でmatplotlibを用いてグラフを描いた時に、グラフ上をクリックしたときにその点での座標を調べたいです。以下は試しに書いてみたコードです。

[code]

    import sys
    from PyQt5 import QtGui,QtCore, QtWidgets,QtMultimedia, QtMultimediaWidgets
    from PyQt5.QtWidgets import QDialog, QApplication, QVBoxLayout
    from PyQt5.QtCore import Qt
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
    from matplotlib.figure import Figure
    import matplotlib.pyplot as plt
    
    import numpy as np
    
    
    class MyCanvas(FigureCanvas):
        def __init__(self, parent=None, width=5, height=4, dpi=100):
            fig = Figure(figsize=(width, height), dpi=dpi)
            self.axes = fig.add_subplot(111)
    
            self.plot_figure()
    
            FigureCanvas.__init__(self, fig)
            self.setParent(parent)
    
    
    
        def mousePressEvent(self,event):
            if event.button() == Qt.LeftButton:
                print("l")
                #print(event.xdata())
                #print(dir(event))
    
            elif event.button() == Qt.RightButton:
                pos=event.pos()
                print(pos.x())
                print(pos.y())
                #print("L")
    
    
    class MyStaticMplCanvas(MyCanvas):
        def plot_figure(self):
            x=np.arange(0,2*np.pi,0.1)
            y=np.sin(x)
            self.axes.plot(x,y,"-")
            self.axes.set_xlabel("x")
            self.axes.set_ylabel("y")
            self.axes.set_xlim(0,2*np.pi) 
    
    
    class Main(QDialog):
        def __init__(self, parent=None):
            super(Main, self).__init__(parent)
    
            #canvas = MyCanvas(self)
            canvas = MyStaticMplCanvas(self)
    
            layout = QVBoxLayout()
            layout.addWidget(canvas)
    
            self.setLayout(layout)
    
            self.setWindowTitle('plot')
            self.show()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        win = Main()
    
        app.exec_()
    
[/code]

これを実行すると次のようなGUIが作成されます。  
[![画像](https://i.stack.imgur.com/7t1ex.png)](https://i.stack.imgur.com/7t1ex.png)  
ここで、グラフ上で左クリックしたときにコンソール上に、クリック点でのグラフ上の座標が表示されるようにしたいです。しかし、どのようにすればよいのか分かりません。


CC BY-SA 4.0
2019-06-17T08:53:06.210
55878
2019-06-18T07:43:03.973
2019-06-18T07:43:03.973
19110
26529
question
python python3 pyqt5
pyqt5でグラフ上をクリックしたときにその点での座標を調べたい


次のコードで動きました。

[code]

    import sys
    from PyQt5 import QtGui,QtCore, QtWidgets,QtMultimedia, QtMultimediaWidgets
    from PyQt5.QtWidgets import QDialog, QApplication, QVBoxLayout
    from PyQt5.QtCore import Qt
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
    from matplotlib.figure import Figure
    import matplotlib.pyplot as plt
    
    import numpy as np
    
    
    class MyCanvas(FigureCanvas):
        def __init__(self, parent=None, width=5, height=4, dpi=100):
            fig = Figure(figsize=(width, height), dpi=dpi)
            self.axes = fig.add_subplot(111)
    
            self.plot_figure()
    
            FigureCanvas.__init__(self, fig)
            self.setParent(parent)
    
            self.mpl_connect('button_press_event', self.on_button_press)
    
        def on_button_press(self,event):
            if event.button == 1: # MouseButton.LEFT:
                print('Left Button')
                print('xdata = ' + str(event.xdata))
                print('ydata = ' + str(event.ydata))
            elif event.button == 3: # MouseButton.RIGHT:
                print('Right Button')
        """    
        def mousePressEvent(self,event):
            if event.button() == Qt.LeftButton:
                print("l")
                #print(event.xdata())
                #print(dir(event))
    
            elif event.button() == Qt.RightButton:
                pos=event.pos()
                print(pos.x())
                print(pos.y())
                #print("L")
        """
    
    
    class MyStaticMplCanvas(MyCanvas):
        def plot_figure(self):
            x=np.arange(0,2*np.pi,0.1)
            y=np.sin(x)
            self.axes.plot(x,y,"-")
            self.axes.set_xlabel("x")
            self.axes.set_ylabel("y")
            self.axes.set_xlim(0,2*np.pi) 
    
    
    class Main(QDialog):
        def __init__(self, parent=None):
            super(Main, self).__init__(parent)
    
            #canvas = MyCanvas(self)
            canvas = MyStaticMplCanvas(self)
    
            layout = QVBoxLayout()
            layout.addWidget(canvas)
    
            self.setLayout(layout)
    
            self.setWindowTitle('plot')
            self.show()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        win = Main()
    
        app.exec_()
    
[/code]


CC BY-SA 4.0
2019-06-18T07:39:44.683
55895
2019-06-18T07:39:44.683
26529
55878
answer