在新窗口中显示matplotlib图,第二个窗口不显示结果pyqt

时间:2020-10-21 16:18:08

标签: python matplotlib pyqt5

我正在使用picot GUI应用程序显示一些图形和图表,但我尝试使用pyqtgraph来完成此操作,但是它不适用于日期或时间,因此我切换到matplotlib

它工作正常,但结果显示在同一窗口中,我无法回到主窗口

我正在寻找一种在另一个窗口中显示相同结果的方法,因为我尝试使用ui内部的示例https://github.com/swharden/Python-GUI-examples/tree/master/2016-07-30_qt_matplotlib_sine_scroll这样的小部件来完成此操作,但是它存在很多问题

我尝试使用这个示例matplotlib in second window pyqt5 它工作正常,并显示了另一个窗口,但是它是空的

这是我的用户界面

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton_113">
    <property name="geometry">
     <rect>
      <x>610</x>
      <y>90</y>
      <width>161</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Recherche</string>
    </property>
   </widget>
   <widget class="QDateEdit" name="dateEdit_20">
    <property name="geometry">
     <rect>
      <x>340</x>
      <y>90</y>
      <width>251</width>
      <height>41</height>
     </rect>
    </property>
    <property name="dateTime">
     <datetime>
      <hour>0</hour>
      <minute>0</minute>
      <second>0</second>
      <year>2020</year>
      <month>1</month>
      <day>1</day>
     </datetime>
    </property>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
   <widget class="QDateEdit" name="dateEdit_19">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>90</y>
      <width>271</width>
      <height>41</height>
     </rect>
    </property>
    <property name="dateTime">
     <datetime>
      <hour>0</hour>
      <minute>0</minute>
      <second>0</second>
      <year>2020</year>
      <month>1</month>
      <day>1</day>
     </datetime>
    </property>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

这是运行matplotlib图的函数

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import pandas as pd
from pandas import *
from datetime import datetime 

import matplotlib.pyplot as plt

import matplotlib.dates as mdates
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import psycopg2

from admin import Ui_MainWindow as ui

class MplCanvas(FigureCanvasQTAgg):
    
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)

class SecondWindow(QMainWindow):
    def __init__(self):
         super(SecondWindow, self).__init__()
         self.main_widget = QtWidgets.QWidget()
         self.setCentralWidget(self.main_widget)

         layout = QtWidgets.QVBoxLayout(self.main_widget)
         sc = MplCanvas(self.main_widget, width = 300, height = 300)
         layout.addWidget(sc)

class MainApp(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.Handel_Buttons()

    def Handel_Buttons(self):
        self.pushButton_113.clicked.connect(self.draw_graph_all)

    def draw_graph_all(self): #pushButton_113
            self.connection = psycopg2.connect(user="postgres",
                                            password="password",
                                            host="localhost",
                                            database="database")
            self.cur = self.connection.cursor()

            date_0 = str(self.dateEdit_19.text())
            date_1 = str(self.dateEdit_20.text())


            self.cur.execute( '''SELECT date_d, SUM(montant) FROM transactions WHERE date_d BETWEEN %s AND %s  GROUP BY date_d ''', (date_0, date_1))
            rows = self.cur.fetchall()

            date = []
            montant = []

            for row in rows:
                date.append(row[0])
                montant.append(row[1])

            self.SW = SecondWindow()
            sc = MplCanvas(self, width=5, height=4, dpi=100)
            sc.axes.plot(date, montant)
        
            self.SW.resize(300,300)
            self.SW.show()

1 个答案:

答案 0 :(得分:0)

我找到了在新的单独窗口中显示matplotlib图的解决方案

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import pandas as pd
from pandas import *
from datetime import datetime 

import matplotlib.pyplot as plt

import matplotlib.dates as mdates
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import psycopg2

from admin import Ui_MainWindow as ui


class MainApp(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.Handel_Buttons()

    def Handel_Buttons(self):
        self.pushButton_113.clicked.connect(self.draw_graph_all)

    def draw_graph_all(self): #pushButton_113
            self.connection = psycopg2.connect(user="postgres",
                                            password="password",
                                            host="localhost",
                                            database="database")
            self.cur = self.connection.cursor()

            date_0 = str(self.dateEdit_19.text())
            date_1 = str(self.dateEdit_20.text())


            self.cur.execute( '''SELECT date_d, SUM(montant) FROM transactions WHERE date_d BETWEEN %s AND %s  GROUP BY date_d ''', (date_0, date_1))
            rows = self.cur.fetchall()

            date = []
            montant = []

            for row in rows:
                date.append(row[0])
                montant.append(row[1])

            plt.figure()
            plt.plot(date, montant)
            plt.show(block=False)