Home > Net >  matplotlib event doesn't work when I use button clicked connect in pyqt5
matplotlib event doesn't work when I use button clicked connect in pyqt5

Time:11-25

I have 2 class, one (Plot) is for plot matplotlib figure, another (Widget) is for pyqt5.

When I create a button in pyqt5 and clicked_connect to class Plot to create figure,

the button_press_event in Plot doesn't work.

import pandas as pd 
import numpy as np 
from PyQt5.QtWidgets import * 
import matplotlib.pyplot as plt 
import sys 

# x, y data 
x = np.random.randint(50, size=10)
y = np.random.randint(50, size=10)

class Plot:
    def __init__(self):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.scatter(x, y)
        fig.canvas.mpl_connect('button_press_event', self.on_press)
        plt.show()

    def on_press(self, event):
        print(event.ydata)

class Widget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    
    def initUI(self):
        self.btn = QPushButton('button', self)
        self.btn.clicked.connect(Plot) 
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

If I want to use event in Plot by button clicked connect in pyqt5, How can I do?

I don't want to use FigureCanvas to plot figure in pyqt5 window,

because I need a full screen figure to do something.

CodePudding user response:

for plotting a graph with PyQt you need an element to render it. This is the Figure Canvas. Using layout options and properties you can customize it to be full screen. I created an example where you can click on the button to get a plot in fullscreen. If you need the navigation toolbar you have to decide yourself. I find this always very handy.

import numpy as np 
import PyQt5.QtWidgets as qtw 
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt 
import sys 

class PlotEx(qtw.QWidget):
    def __init__(self):
        super().__init__()
    
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)
        self.button = qtw.QPushButton('Plot')
    
        layout = qtw.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.showMaximized()
    
        self.button.clicked.connect(self.plot)

    def plot(self):
        x = np.random.randint(50, size=10)
        y = np.random.randint(50, size=10)
        self.figure.clear()
        ax = self.figure.add_subplot(111)
        ax.scatter(x,y)
        self.canvas.draw()

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    main = PlotEx()
    main.show()
    sys.exit(app.exec_())

If you need the plot in a second screen, then you have to create another class, which you then call by clicking the button.

  • Related