xpress_embedo
xpress_embedo

Reputation: 377

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. 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)

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.

Upvotes: 0

Views: 2364

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339795

You're mixing matplotlib.pyplot's show GUI with another PyQt GUI. The problem is that the figure to show in the matplotlib GUI is created only once. As soon as it's closed, it's lost.

The simple solution is to create it within the generate_graph function. 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()

Upvotes: 1

Related Questions