偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python入門(mén)圖文教程 - 如何在PyQt5中嵌入Matplotlib生成的圖像

開(kāi)發(fā) 后端
在這篇PyQt5文章中,我將向您展示如何在PyQt5中嵌入Matplotlib生成的圖像。

在這篇PyQt5文章中,我將向您展示如何在PyQt5中嵌入Matplotlib生成的圖像。

什么是Matplotlib?

Matplotlib是一個(gè)Python 2D繪圖庫(kù),它以各種硬拷貝格式和跨平臺(tái)的交互環(huán)境生成出版質(zhì)量圖形。Matplotlib可以用于Python腳本、Python和IPython shell、Jupyter筆記本、Web應(yīng)用程序服務(wù)器和四個(gè)圖形用戶界面工具包。

Matplotlib試圖讓事情變得簡(jiǎn)單,讓復(fù)雜的事情變得可能。您可以生成繪圖,直方圖,功率譜,柱狀圖,錯(cuò)誤圖,散點(diǎn)圖等,只需幾行代碼。

為了進(jìn)行簡(jiǎn)單的繪圖,pyplot模塊提供了一個(gè)類(lèi)似matlab的接口,特別是在與IPython結(jié)合使用時(shí)。對(duì)于高級(jí)用戶,您可以通過(guò)一個(gè)面向?qū)ο蟮慕缑婊蛞唤MMATLAB用戶熟悉的函數(shù)來(lái)完全控制線條樣式、字體屬性、軸屬性等。

安裝

您可以通過(guò)使用pip install matplotlib來(lái)簡(jiǎn)單安裝matplotlib。

什么是PyQt5 ?

Qt是一組跨平臺(tái)的C++庫(kù),這些庫(kù)實(shí)現(xiàn)了用于訪問(wèn)現(xiàn)代桌面和移動(dòng)系統(tǒng)的許多方面的高級(jí)api。這些包括位置和定位服務(wù),多媒體,NFC和藍(lán)牙連接,一個(gè)基于鉻的web瀏覽器,以及傳統(tǒng)的UI開(kāi)發(fā)。

PyQt5是針對(duì)Qt v5的一組全面的Python綁定。它被實(shí)現(xiàn)為35個(gè)以上的擴(kuò)展模塊,使Python可以在包括iOS和Android在內(nèi)的所有支持平臺(tái)上作為C++的替代應(yīng)用開(kāi)發(fā)語(yǔ)言。

PyQt5還可以嵌入到基于C++的應(yīng)用程序中,以允許這些應(yīng)用程序的用戶配置或增強(qiáng)這些應(yīng)用程序的功能。

安裝

GPL版本的PyQt5可以從PyPI安裝:

  1. pip install PyQt5 

包括Qt的LGPL版本所需部件的副本。

pip還將從sdist包構(gòu)建和安裝綁定,但Qt的qmake工具必須在PATH上。

sip安裝工具還將安裝來(lái)自sdist包的綁定,但允許您配置安裝的許多方面。

現(xiàn)在,這是如何在PyQt5中嵌入Matplotlib生成圖像的完整代碼。 

  1. from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton  
  2. import sys  
  3. from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas  
  4. from matplotlib.figure import Figure  
  5. import numpy as np  
  6. class Window(QMainWindow):  
  7.     def __init__(self):  
  8.         super().__init__()  
  9.         title = "在PyQt5中嵌入Matplotlib - www.linuxmi.com"  
  10.         top = 400  
  11.         left = 400  
  12.         width = 1000  
  13.         height = 600  
  14.         self.setWindowTitle(title)  
  15.         self.setGeometry(top, left, width, height)  
  16.         self.MyUI()  
  17.     def MyUI(self):  
  18.         canvas = Canvas(self, width=8height=4 
  19.         canvas.move(0,0)  
  20.         button = QPushButton("點(diǎn)擊我", self)  
  21.         button.move(100, 500)  
  22.         button2 = QPushButton("再次點(diǎn)擊我", self)  
  23.         button2.move(350, 500)  
  24. class Canvas(FigureCanvas):  
  25.     def __init__(self, parent = Nonewidth = 5height = 5dpi = 100):  
  26.         fig = Figure(figsize=(width, height), dpidpi=dpi)  
  27.         self.axes = fig.add_subplot(111)  
  28.         FigureCanvas.__init__(self, fig)  
  29.         self.setParent(parent)  
  30.         self.plot()  
  31.     def plot(self):  
  32.         x = np.array([50,30,40,20])  
  33.         labels = ["LinuxMi.com", "Debian", "Linux", "Python"]  
  34.         ax = self.figure.add_subplot(111)  
  35.         ax.pie(x, labelslabels=labels)  
  36. app = QApplication(sys.argv)  
  37. window = Window()  
  38. window.show()  
  39. app.exec() 

我們導(dǎo)入了所需的庫(kù),基本上是我們需要的 

  1. from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton  
  2. import sys  
  3. from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas  
  4. from matplotlib.figure import Figure  
  5. import numpy as np 

這是我們的主窗口類(lèi)它繼承自QMainWindow,我們對(duì)窗口有一些要求,比如窗口的標(biāo)題,寬度,高度,我們還在這個(gè)類(lèi)中調(diào)用了MyUI()方法。 

  1. class Window(QMainWindow):  
  2.     def __init__(self):  
  3.         super().__init__()  
  4.         title = "在PyQt5中嵌入Matplotlib - www.linuxmi.com"  
  5.         top = 400 
  6.         left = 400  
  7.         width = 1000  
  8.         height = 600  
  9.         self.setWindowTitle(title)  
  10.         self.setGeometry(top, left, width, height)  
  11.         self.MyUI() 

在這個(gè)方法中,我們創(chuàng)建了一個(gè)帶有兩個(gè)QPushButton的Canvas。   

  1. def MyUI(self):  
  2.         canvas = Canvas(self, width=8height=4 
  3.         canvas.move(0,0)  
  4.         button = QPushButton("點(diǎn)擊我", self)  
  5.         button.move(100, 500)  
  6.         button2 = QPushButton("再次點(diǎn)擊我", self)  
  7.         button2.move(350, 500) 

和這是我們的Canvas類(lèi),繼承自FigureCanvas。 

  1. class Canvas(FigureCanvas):  
  2.     def __init__(self, parent = Nonewidth = 5height = 5dpi = 100):  
  3.         fig = Figure(figsize=(width, height), dpidpi=dpi)  
  4.         self.axes = fig.add_subplot(111)  
  5.         FigureCanvas.__init__(self, fig)  
  6.         self.setParent(parent)  
  7.         self.plot() 

在這里,我們還將在PyQt5窗口中繪制一個(gè)餅狀圖。 

  1. def plot(self):  
  2.       x = np.array([50,30,40,20])  
  3.       labels = ["LinuxMi.com", "Debian", "Linux", "Python"] 
  4.       ax = self.figure.add_subplot(111)  
  5.       ax.pie(x, labelslabels=labels) 

因此在這里,每個(gè)PyQt5應(yīng)用程序都必須創(chuàng)建一個(gè)應(yīng)用程序?qū)ο?。sys.argv參數(shù)是命令行的參數(shù)列表。 

  1. app = QApplication(sys.argv) 

最后,我們進(jìn)入應(yīng)用程序的主循環(huán)。事件處理從這里開(kāi)始。

mainloop從窗口系統(tǒng)接收事件并將它們分派給應(yīng)用程序小部件。 

  1. app.exec()  
  2. sys.exit() 

運(yùn)行完整的代碼,結(jié)果如下:

 

 

責(zé)任編輯:龐桂玉 來(lái)源: Linux公社
相關(guān)推薦

2021-07-09 12:37:31

GoPython編程語(yǔ)言

2023-07-10 07:10:34

2013-01-23 16:39:12

VMware vCen

2023-11-23 13:10:24

Python框架

2013-01-25 11:11:58

VMwarevCenter Pro

2011-09-06 10:10:12

MTK系統(tǒng)Android系統(tǒng)

2010-03-26 16:18:44

CentOS系統(tǒng)

2011-08-16 10:01:02

2013-11-21 10:06:35

2010-09-25 16:47:23

DHCP中繼代理配置

2011-03-30 10:31:10

HostEase數(shù)據(jù)庫(kù)

2012-05-15 14:00:51

WP7開(kāi)發(fā)環(huán)境

2022-09-19 07:08:28

dockerRedisCentos8

2014-03-31 09:26:13

2020-07-06 15:50:41

Python文件Linux

2022-08-08 09:55:30

PythonPyQt5圖形界面

2023-11-30 20:51:26

多子圖布局matplotlib

2011-12-22 14:36:36

PhoneGapWindows Pho環(huán)境搭建

2013-12-23 14:46:11

Windows 8.1

2012-03-31 15:54:44

路由器接路由路由器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)