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