Python入門圖文教程 - 如何在PyQt5中嵌入Matplotlib生成的圖像
在這篇PyQt5文章中,我將向您展示如何在PyQt5中嵌入Matplotlib生成的圖像。
什么是Matplotlib?
Matplotlib是一個Python 2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互環(huán)境生成出版質(zhì)量圖形。Matplotlib可以用于Python腳本、Python和IPython shell、Jupyter筆記本、Web應用程序服務器和四個圖形用戶界面工具包。
Matplotlib試圖讓事情變得簡單,讓復雜的事情變得可能。您可以生成繪圖,直方圖,功率譜,柱狀圖,錯誤圖,散點圖等,只需幾行代碼。
為了進行簡單的繪圖,pyplot模塊提供了一個類似matlab的接口,特別是在與IPython結(jié)合使用時。對于高級用戶,您可以通過一個面向?qū)ο蟮慕缑婊蛞唤MMATLAB用戶熟悉的函數(shù)來完全控制線條樣式、字體屬性、軸屬性等。
安裝
您可以通過使用pip install matplotlib來簡單安裝matplotlib。
什么是PyQt5 ?
Qt是一組跨平臺的C++庫,這些庫實現(xiàn)了用于訪問現(xiàn)代桌面和移動系統(tǒng)的許多方面的高級api。這些包括位置和定位服務,多媒體,NFC和藍牙連接,一個基于鉻的web瀏覽器,以及傳統(tǒng)的UI開發(fā)。
PyQt5是針對Qt v5的一組全面的Python綁定。它被實現(xiàn)為35個以上的擴展模塊,使Python可以在包括iOS和Android在內(nèi)的所有支持平臺上作為C++的替代應用開發(fā)語言。
PyQt5還可以嵌入到基于C++的應用程序中,以允許這些應用程序的用戶配置或增強這些應用程序的功能。
安裝
GPL版本的PyQt5可以從PyPI安裝:
- pip install PyQt5
包括Qt的LGPL版本所需部件的副本。
pip還將從sdist包構(gòu)建和安裝綁定,但Qt的qmake工具必須在PATH上。
sip安裝工具還將安裝來自sdist包的綁定,但允許您配置安裝的許多方面。
現(xiàn)在,這是如何在PyQt5中嵌入Matplotlib生成圖像的完整代碼。
- from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
- import sys
- from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
- from matplotlib.figure import Figure
- import numpy as np
- class Window(QMainWindow):
- def __init__(self):
- super().__init__()
- title = "在PyQt5中嵌入Matplotlib - www.linuxmi.com"
- top = 400
- left = 400
- width = 1000
- height = 600
- self.setWindowTitle(title)
- self.setGeometry(top, left, width, height)
- self.MyUI()
- def MyUI(self):
- canvas = Canvas(self, width=8, height=4)
- canvas.move(0,0)
- button = QPushButton("點擊我", self)
- button.move(100, 500)
- button2 = QPushButton("再次點擊我", self)
- button2.move(350, 500)
- class Canvas(FigureCanvas):
- def __init__(self, parent = None, width = 5, height = 5, dpi = 100):
- fig = Figure(figsize=(width, height), dpidpi=dpi)
- self.axes = fig.add_subplot(111)
- FigureCanvas.__init__(self, fig)
- self.setParent(parent)
- self.plot()
- def plot(self):
- x = np.array([50,30,40,20])
- labels = ["LinuxMi.com", "Debian", "Linux", "Python"]
- ax = self.figure.add_subplot(111)
- ax.pie(x, labelslabels=labels)
- app = QApplication(sys.argv)
- window = Window()
- window.show()
- app.exec()
我們導入了所需的庫,基本上是我們需要的
- from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
- import sys
- from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
- from matplotlib.figure import Figure
- import numpy as np
這是我們的主窗口類它繼承自QMainWindow,我們對窗口有一些要求,比如窗口的標題,寬度,高度,我們還在這個類中調(diào)用了MyUI()方法。
- class Window(QMainWindow):
- def __init__(self):
- super().__init__()
- title = "在PyQt5中嵌入Matplotlib - www.linuxmi.com"
- top = 400
- left = 400
- width = 1000
- height = 600
- self.setWindowTitle(title)
- self.setGeometry(top, left, width, height)
- self.MyUI()
在這個方法中,我們創(chuàng)建了一個帶有兩個QPushButton的Canvas。
- def MyUI(self):
- canvas = Canvas(self, width=8, height=4)
- canvas.move(0,0)
- button = QPushButton("點擊我", self)
- button.move(100, 500)
- button2 = QPushButton("再次點擊我", self)
- button2.move(350, 500)
和這是我們的Canvas類,繼承自FigureCanvas。
- class Canvas(FigureCanvas):
- def __init__(self, parent = None, width = 5, height = 5, dpi = 100):
- fig = Figure(figsize=(width, height), dpidpi=dpi)
- self.axes = fig.add_subplot(111)
- FigureCanvas.__init__(self, fig)
- self.setParent(parent)
- self.plot()
在這里,我們還將在PyQt5窗口中繪制一個餅狀圖。
- def plot(self):
- x = np.array([50,30,40,20])
- labels = ["LinuxMi.com", "Debian", "Linux", "Python"]
- ax = self.figure.add_subplot(111)
- ax.pie(x, labelslabels=labels)
因此在這里,每個PyQt5應用程序都必須創(chuàng)建一個應用程序?qū)ο?。sys.argv參數(shù)是命令行的參數(shù)列表。
- app = QApplication(sys.argv)
最后,我們進入應用程序的主循環(huán)。事件處理從這里開始。
mainloop從窗口系統(tǒng)接收事件并將它們分派給應用程序小部件。
- app.exec()
- sys.exit()
運行完整的代碼,結(jié)果如下: