如何在小部件中使用带有 pandas.plot() 的 matplotlib 图? [英] How can I have the matplotlib graph with pandas.plot() in the widget?

查看:47
本文介绍了如何在小部件中使用带有 pandas.plot() 的 matplotlib 图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我有这个代码:执行时,您将看到两个主窗口和另一个称为图 1 的窗口,当读取 csv 数据并按下绘图按钮时,该图显示在另一个窗口中(图 2)我该如何解决这个问题?非常感谢您的宝贵时间!

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

推荐答案

plot() Pandas 函数有一个可选的属性叫做ax:

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

ax : matplotlib 轴对象,默认无

ax : matplotlib axes object, default None

如果我们希望它在 FigureCanvas() 中绘制,您必须首先创建一些轴,并将其作为 ax 中的属性传递:

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

这篇关于如何在小部件中使用带有 pandas.plot() 的 matplotlib 图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆