使用 QT5 调整 MatPlotLib 图表的大小 python
Resizing MatPlotLib chart with QT5 python
我试图让我的 matplotlib 图适合它所在的 qt5 小部件的大小。为了做到这一点,我制作了一个调整大小事件处理程序,但我不确定如何更改 matplotlib 已经绘制的图表。有什么建议么?
这是我的代码:
# Form implementation generated from reading ui file 'untitled1.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QApplication, QWidget
class Canvas(FigureCanvas):
def __init__(self, parent):
self.fig, self.ax = plt.subplots(figsize=(4, 4), dpi=100)
super().__init__(self.fig)
self.setParent(parent)
"""
Matplotlib Script
"""
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
self.ax.plot(t, s)
self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
self.ax.grid()
self.ax.plot(t, s+2)
self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
self.ax.grid()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(749, 444)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.MplWidget = QtWidgets.QWidget(self.centralwidget)
self.chart = Canvas(self.MplWidget)
self.MplWidget.setGeometry(QtCore.QRect(0, 0, 700, 440))
self.MplWidget.setObjectName("MplWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 749, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class Window(QtWidgets.QMainWindow):
resized = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.resized.connect(self.resizeFunction)
def resizeEvent(self, event):
self.resized.emit()
return super(Window, self).resizeEvent(event)
def resizeFunction(self):
self.ui.frame.move(self.width()-171, 0)
self.ui.MplWidget.setGeometry(0, 0, self.width()-171, self.width()-171)
# I would like to resize chart here <-----
print("someFunction")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
screen = app.screens()[0]
_dpi = screen.physicalDotsPerInch()
print(_dpi)
w = Window()
w.show()
sys.exit(app.exec_())
此外,由于本指南,我已将 matplotlib 集成到 QT5 中:https://learndataanalysis.org/embed-matplotlib-graph-in-a-pyqt5-application/
用此代码替换您的 resizeFunction
函数
def resizeFunction(self):
# self.ui.MplWidget.setGeometry(0, 0, self.width()-171, self.width()-171)
self.ui.MplWidget.setGeometry(QtCore.QRect(0, 0, self.width(), self.height()-20))
self.ui.chart.setGeometry(QtCore.QRect(0, 0, self.width(), self.height()-30))
# I would like to resize chart here <-----
print("someFunction")
随意用您想要的任意填充量替换“-30”或“-20”。
我试图让我的 matplotlib 图适合它所在的 qt5 小部件的大小。为了做到这一点,我制作了一个调整大小事件处理程序,但我不确定如何更改 matplotlib 已经绘制的图表。有什么建议么? 这是我的代码:
# Form implementation generated from reading ui file 'untitled1.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QApplication, QWidget
class Canvas(FigureCanvas):
def __init__(self, parent):
self.fig, self.ax = plt.subplots(figsize=(4, 4), dpi=100)
super().__init__(self.fig)
self.setParent(parent)
"""
Matplotlib Script
"""
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
self.ax.plot(t, s)
self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
self.ax.grid()
self.ax.plot(t, s+2)
self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
self.ax.grid()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(749, 444)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.MplWidget = QtWidgets.QWidget(self.centralwidget)
self.chart = Canvas(self.MplWidget)
self.MplWidget.setGeometry(QtCore.QRect(0, 0, 700, 440))
self.MplWidget.setObjectName("MplWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 749, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class Window(QtWidgets.QMainWindow):
resized = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.resized.connect(self.resizeFunction)
def resizeEvent(self, event):
self.resized.emit()
return super(Window, self).resizeEvent(event)
def resizeFunction(self):
self.ui.frame.move(self.width()-171, 0)
self.ui.MplWidget.setGeometry(0, 0, self.width()-171, self.width()-171)
# I would like to resize chart here <-----
print("someFunction")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
screen = app.screens()[0]
_dpi = screen.physicalDotsPerInch()
print(_dpi)
w = Window()
w.show()
sys.exit(app.exec_())
此外,由于本指南,我已将 matplotlib 集成到 QT5 中:https://learndataanalysis.org/embed-matplotlib-graph-in-a-pyqt5-application/
用此代码替换您的 resizeFunction
函数
def resizeFunction(self):
# self.ui.MplWidget.setGeometry(0, 0, self.width()-171, self.width()-171)
self.ui.MplWidget.setGeometry(QtCore.QRect(0, 0, self.width(), self.height()-20))
self.ui.chart.setGeometry(QtCore.QRect(0, 0, self.width(), self.height()-30))
# I would like to resize chart here <-----
print("someFunction")
随意用您想要的任意填充量替换“-30”或“-20”。