Python 在新窗口中显示matplotlib图形,第二个窗口不显示结果pyqt
我正在使用picot GUI应用程序来显示一些图形和图表,我尝试使用pyqtgraph来显示,但它不适用于日期或时间,因此我切换到matplotlib 它工作得很好,但是结果显示在同一个窗口中,我无法回到主窗口 我正在寻找一种在另一个窗口中显示相同结果的方法,因为我尝试在ui中使用类似于此示例的小部件,但它有很多问题 我试着用这个例子 它工作得很好,它有另一个窗口,但它是空的 这是我的用户界面Python 在新窗口中显示matplotlib图形,第二个窗口不显示结果pyqt,python,matplotlib,pyqt5,Python,Matplotlib,Pyqt5,我正在使用picot GUI应用程序来显示一些图形和图表,我尝试使用pyqtgraph来显示,但它不适用于日期或时间,因此我切换到matplotlib 它工作得很好,但是结果显示在同一个窗口中,我无法回到主窗口 我正在寻找一种在另一个窗口中显示相同结果的方法,因为我尝试在ui中使用类似于此示例的小部件,但它有很多问题 我试着用这个例子 它工作得很好,它有另一个窗口,但它是空的 这是我的用户界面 <?xml version="1.0" encoding="UTF
<?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 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)
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)