简体   繁体   English

当我在 pyqt5 中使用按钮单击连接时,matplotlib 事件不起作用

[英]matplotlib event doesn't work when I use button clicked connect in pyqt5

I have 2 class, one (Plot) is for plot matplotlib figure, another (Widget) is for pyqt5.我有 2 个 class,一个(Plot)用于 plot matplotlib 图,另一个(Widget)用于 pyqt5。

When I create a button in pyqt5 and clicked_connect to class Plot to create figure,当我在 pyqt5 中创建一个按钮并单击连接到 class Plot 以创建图形时,

the button_press_event in Plot doesn't work. Plot 中的 button_press_event 不起作用。

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?如果我想通过在 pyqt5 中单击连接按钮来使用 Plot 中的事件,我该怎么做?

I don't want to use FigureCanvas to plot figure in pyqt5 window,我不想使用 FigureCanvas 到 pyqt5 window 中的 plot 图,

because I need a full screen figure to do something.因为我需要一个全屏图形来做某事。

for plotting a graph with PyQt you need an element to render it.要使用 PyQt 绘制图形,您需要一个元素来呈现它。 This is the Figure Canvas. Using layout options and properties you can customize it to be full screen.这是图 Canvas。使用布局选项和属性,您可以将其自定义为全屏。 I created an example where you can click on the button to get a plot in fullscreen.我创建了一个示例,您可以在其中单击按钮以全屏显示 plot。 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.如果您需要在第二个屏幕中使用 plot,则必须创建另一个 class,然后单击按钮调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM