0

So far I have this code: When it's executed, you'll see two windows main and another one called Figure 1, when the csv data is readed, and the plot button pressed, The graph is shown in another window (Figure 2) How can I fix this issues? Many, many thanks in advance for your time!

import sys
import pandas as pd
import matplotlib
matplotlib.use('QT5Agg')
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(600,300, 1000, 600)
        self.center()
        grid = QtWidgets.QGridLayout()
        widget = QtWidgets.QWidget(self)
        self.setCentralWidget(widget)
        widget.setLayout(grid)
         #Import CSV Button
        btn1 = QtWidgets.QPushButton('Import CSV', self)
        btn1.resize(btn1.sizeHint())
        btn1.clicked.connect(self.getCSV)
        grid.addWidget(btn1, 1, 0)

        self.figure = plt.figure(figsize=(15,5))    
        self.canvas = FigureCanvas(self.figure)     
        grid.addWidget(self.canvas, 2,0,1,2)


        btn2 = QtWidgets.QPushButton('Plot', self)
        btn2.resize(btn2.sizeHint())    
        btn2.clicked.connect(self.plot)
        grid.addWidget(btn2, 1, 1)
        self.show()

    def plot(self):
        self.df.plot(x='col1',y='col2')
        self.canvas.draw()

    def getCSV(self):
         filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '/home')
         if filePath != "":
            print ("Dirección",filePath)
            self.df = pd.read_csv(str(filePath))

    def center(self):
        qr = self.frameGeometry()
        cp = QtWidgets.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    sys.exit(app.exec_())


if __name__ == '__main__':
   main()

1 Answer 1

1

the plot() function of pandas has an optional attribute called ax:

ax : matplotlib axes object, default None

If we want it to be drawn inside FigureCanvas() you must first create some axes, and pass it as an attribute in ax:

def plot(self):
    self.figure.clear()
    ax =  self.figure.add_subplot(111) 
    self.df.plot(x='col1',y='col2', ax=ax)
    self.canvas.draw()
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks again for your help! Worked very nice! Any ideas to get rid of the Figure 1, that appears at the same time as the main window?
I do not understand you, could you explain to me what you want to get? :P
Gladly :D ! When I execute the code, I'm getting two windows (here the screenshot: drive.google.com/file/d/163lRSxyApUAwRJEmdSaQOBIDnt1oLANS/… )
I find it strange that this happens to you, I could help you through teamviewer since that error does not occur on my pc.
I'm using Ubuntu and Spyder, maybe something there is generating the error, let me see if I can fix it ^_^ Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.