【问题标题】:show matplotlib graph in a new window , second window dont show result pyqt在新窗口中显示 matplotlib 图,第二个窗口不显示结果 pyqt
【发布时间】:2020-10-21 16:18:08
【问题描述】:

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

效果很好,但结果显示在同一个窗口中,我无法返回主窗口

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

我试试这个例子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()

【问题讨论】:

    标签: python matplotlib pyqt5


    【解决方案1】:

    我找到了在新的分隔窗口中显示 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)
                
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2015-01-12
      • 1970-01-01
      • 2015-12-01
      • 2015-11-20
      相关资源
      最近更新 更多