將matplotlib繪圖嵌入pyqt的方法示例
更新時間:2020年01月08日 11:54:51 作者:lemonade_117
這篇文章主要介紹了將matplotlib繪圖嵌入pyqt的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
我的終極整理,供參考
# coding:utf-8 import matplotlib # 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas繼承自QtWidgets.QWidget) from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout import matplotlib.pyplot as plt import numpy as np import sys """學(xué)好pyplot API和面向?qū)ο?API搞定matplotlib繪圖顯示在GUI界面上""" class Main_window(QDialog): def __init__(self): super().__init__() # 三步走,定Figure,定Axes,定FigureCanvas # 1 直接一段代碼搞定figure和axes self.figure, (self.ax1, self.ax2) = plt.subplots(figsize=(13, 3), ncols=2) # 2 先創(chuàng)建figure再創(chuàng)建axes # 2.1 用plt.figure() / Figure() 創(chuàng)建figure, 推薦前者 self.figure = plt.figure(figsize=(5,3), facecolor='#FFD7C4') # self.figure = Figure(figsize=(5,3), facecolor='#FFD7C4') # 2.2 用plt.subplots() / plt.add_subplot() 創(chuàng)建axes, 推薦前者 (self.ax1, self.ax2) = self.figure.subplots(1, 2) # ax1 = self.figure.add_subplot(121) # ax2 = self.figure.add_subplot(122) # 3 綁定figure到canvas上 self.canvas = FigureCanvas(self.figure) self.button_draw = QPushButton("繪圖") self.button_draw.clicked.connect(self.Draw) # 設(shè)置布局 layout = QVBoxLayout() layout.addWidget(self.canvas) layout.addWidget(self.button_draw) self.setLayout(layout) def Draw(self): AgeList = ['10', '21', '12', '14', '25'] NameList = ['Tom', 'Jon', 'Alice', 'Mike', 'Mary'] # 將AgeList中的數(shù)據(jù)轉(zhuǎn)化為int類型 AgeList = list(map(int, AgeList)) # 將x,y轉(zhuǎn)化為numpy數(shù)據(jù)類型,對于matplotlib很重要 self.x = np.arange(len(NameList)) + 1 self.y = np.array(AgeList) # tick_label后邊跟x軸上的值,(可選選項:color后面跟柱型的顏色,width后邊跟柱體的寬度) self.ax1.bar(range(len(NameList)), AgeList, tick_label=NameList, color='green', width=0.5) for a, b in zip(self.x, self.y): self.ax1.text(a-1, b, '%d' % b, ha='center', va='bottom') plt.title("Demo") pos = self.ax2.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r) self.figure.colorbar(pos, ax=self.ax2) # 終于可以用colorbar了 self.canvas.draw() # 運行程序 if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) main_window = Main_window() main_window.show() app.exec()
總結(jié)就是,想要在特定的位置放matplotlib繪圖還是要用面向?qū)ο蟮腁PI,但混合使用pyplot的API可以使代碼更簡單。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python繪圖庫Matplotlib的安裝
- Python利用matplotlib.pyplot繪圖時如何設(shè)置坐標(biāo)軸刻度
- python的繪圖工具matplotlib使用實例
- python matplotlib繪圖,修改坐標(biāo)軸刻度為文字的實例
- python使用matplotlib繪圖時圖例顯示問題的解決
- Python繪圖Matplotlib之坐標(biāo)軸及刻度總結(jié)
- Python的matplotlib繪圖如何修改背景顏色的實現(xiàn)
- 完美解決Python matplotlib繪圖時漢字顯示不正常的問題
- Python使用matplotlib簡單繪圖示例
- matplotlib.pyplot繪圖顯示控制方法
相關(guān)文章
python實現(xiàn)與arduino的串口通信的示例代碼
本文主要介紹了python實現(xiàn)與arduino的串口通信的示例代碼, 在Python中,我們可以使用pyserial庫來實現(xiàn)與Arduino的串口通信,下面就來介紹一下如何使用,感興趣的可以了解一下2024-01-01一文帶你深入理解Python的`functools.lru_cache`裝飾器
Python中的functools.lru_cache裝飾器是一個非常有用的裝飾器,它可以幫助我們優(yōu)化遞歸函數(shù),避免重復(fù)計算已經(jīng)計算過的值,在這篇文章中,我們將探討?functools.lru_cache?的工作原理以及如何使用它,感興趣的朋友跟著小編一起來學(xué)習(xí)吧2023-07-07python實現(xiàn)從字符串中找出字符1的位置以及個數(shù)的方法
這篇文章主要介紹了python實現(xiàn)從字符串中找出字符1的位置以及個數(shù)的方法,對于Python字符串操作的學(xué)習(xí)有一定的幫助與借鑒作用,需要的朋友可以參考下2014-08-08