简体   繁体   English

通过单击按钮更新使用Matplotlib生成的图

[英]Updating Graph generated using Matplotlib by button click

I am working on a GUI application, which generates graphs using Matplotlib package, for gui design i am using PyQt5. 我正在使用GUI应用程序,该应用程序使用Matplotlib包生成图形,用于GUI设计,我正在使用PyQt5。 In this application users loads the data from a line and then on pressing the generate button, a processed graph is generated, now the problem is that, on closing the graph, when user loads the new data, and press the generate button, graph is not displayed again. 在此应用程序中,用户从一行中加载数据,然后按生成按钮,则生成一个已处理的图形,现在的问题是,在关闭图形时,当用户加载新数据并按生成按钮时,图形为不再显示。

Code

import sys
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton
import numpy as np
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
plt.subplots_adjust(hspace=0)

class window(QMainWindow):

    def __init__(self):
        super(window, self).__init__()
        self.setGeometry(50, 50, 100, 100)
        self.setWindowTitle('Generate Graph')
        self.home()

    def home(self):
        btn = QPushButton('Generate', self)
        btn.clicked.connect(self.generate_graph)
        #btn.resize(100, 100)
        #btn.move(100, 100)
        self.show()

    def generate_graph(self):
        # In real application these points gets updated
        x = [0,1,2,3,4,5,6,7,8,9]
        y1 = [0,1,2,3,4,5,6,7,8,9]
        y2 = [0,1,2,3,4,5,6,7,8,9]
        ax1.plot(x,y1)
        ax2.plot(x,y2)
        plt.show()

def run():
    app = QApplication(sys.argv)
    Gui = window()
    sys.exit(app.exec_())

run()

So i am posting the sample program which can show my problem, in this i created a button and generated two plots. 所以我发布了可以显示我的问题的示例程序,在此我创建了一个按钮并生成了两个图。 (Note: these are two subplots, i created two subplots because, i need to write ylabel on the adjacent axis, so it is a requirement i can't change and it must be like this) (注意:这是两个子图,我创建了两个子图,因为我需要在相邻轴上写ylabel,所以这是我无法更改的要求,它必须像这样)

I pressed the generate button, graph gets generated. 我按下了“生成”按钮,就生成了图形。 I closed the graph, and again pressed the generate button but its not re-generated. 我关闭了该图,然后再次按下了“生成”按钮,但它没有重新生成。 Please suggest me what i can add to make this happen. 请建议我我可以添加些什么来实现这一目标。

Is it possible to generate new graph every-time user presses the generate button, i think this will also solve the problem. 用户每次按下生成按钮都可以生成新图形吗,我认为这也可以解决问题。 Please suggest and thanks in advance. 请提出建议并提前致谢。

I had searched with this topic on this forum, and tried various thing like clearing the axis etc etc, but i think i am doing something wrong as i am new to all this. 我已经在这个论坛上搜索了这个主题,并尝试了各种操作,例如清除轴等,但是我认为我做错了,因为我是新手。

You're mixing matplotlib.pyplot 's show GUI with another PyQt GUI. 您正在将matplotlib.pyplotshow GUI与另一个PyQt GUI混合在一起。 The problem is that the figure to show in the matplotlib GUI is created only once. 问题在于,要在matplotlib GUI中显示的图形仅创建一次。 As soon as it's closed, it's lost. 一旦关闭,它就会丢失。

The simple solution is to create it within the generate_graph function. 简单的解决方案是在generate_graph函数中创建它。 Thereby a new figure is created and shown every time the button is pressed. 因此,每次按下按钮时,都会创建并显示一个新图形。

import sys
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton

import matplotlib.pyplot as plt

class window(QMainWindow):

    def __init__(self):
        super(window, self).__init__()
        self.setGeometry(50, 50, 100, 100)
        self.setWindowTitle('Generate Graph')
        self.home()

    def home(self):
        btn = QPushButton('Generate', self)
        btn.clicked.connect(self.generate_graph)
        self.show()

    def generate_graph(self):
        fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
        plt.subplots_adjust(hspace=0)
        x = [0,1,2,3,4,5,6,7,8,9]
        y1 = [0,1,2,3,4,5,6,7,8,9]
        y2 = [0,1,2,3,4,5,6,7,8,9]
        ax1.plot(x,y1)
        ax2.plot(x,y2)
        plt.show()

def run():
    app = QApplication(sys.argv)
    Gui = window()
    sys.exit(app.exec_())

run()

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

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