將matplotlib繪圖嵌入pyqt的方法示例
我的終極整理,供參考
# 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類(lèi)型 AgeList = list(map(int, AgeList)) # 將x,y轉(zhuǎn)化為numpy數(shù)據(jù)類(lèi)型,對(duì)于matplotlib很重要 self.x = np.arange(len(NameList)) + 1 self.y = np.array(AgeList) # tick_label后邊跟x軸上的值,(可選選項(xiàng):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() # 運(yùn)行程序 if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) main_window = Main_window() main_window.show() app.exec()
總結(jié)就是,想要在特定的位置放matplotlib繪圖還是要用面向?qū)ο蟮腁PI,但混合使用pyplot的API可以使代碼更簡(jiǎn)單。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python繪圖庫(kù)Matplotlib的安裝
- Python利用matplotlib.pyplot繪圖時(shí)如何設(shè)置坐標(biāo)軸刻度
- python的繪圖工具matplotlib使用實(shí)例
- python matplotlib繪圖,修改坐標(biāo)軸刻度為文字的實(shí)例
- python使用matplotlib繪圖時(shí)圖例顯示問(wèn)題的解決
- Python繪圖Matplotlib之坐標(biāo)軸及刻度總結(jié)
- Python的matplotlib繪圖如何修改背景顏色的實(shí)現(xiàn)
- 完美解決Python matplotlib繪圖時(shí)漢字顯示不正常的問(wèn)題
- Python使用matplotlib簡(jiǎn)單繪圖示例
- matplotlib.pyplot繪圖顯示控制方法
相關(guān)文章
python實(shí)現(xiàn)與arduino的串口通信的示例代碼
本文主要介紹了python實(shí)現(xiàn)與arduino的串口通信的示例代碼, 在Python中,我們可以使用pyserial庫(kù)來(lái)實(shí)現(xiàn)與Arduino的串口通信,下面就來(lái)介紹一下如何使用,感興趣的可以了解一下2024-01-01python使用梯度下降算法實(shí)現(xiàn)一個(gè)多線(xiàn)性回歸
這篇文章主要為大家詳細(xì)介紹了python使用梯度下降算法實(shí)現(xiàn)一個(gè)多線(xiàn)性回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03如何通過(guò)Flask的request對(duì)象獲取url
這篇文章主要介紹了如何通過(guò)Flask的request對(duì)象獲取url問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07一文帶你深入理解Python的`functools.lru_cache`裝飾器
Python中的functools.lru_cache裝飾器是一個(gè)非常有用的裝飾器,它可以幫助我們優(yōu)化遞歸函數(shù),避免重復(fù)計(jì)算已經(jīng)計(jì)算過(guò)的值,在這篇文章中,我們將探討?functools.lru_cache?的工作原理以及如何使用它,感興趣的朋友跟著小編一起來(lái)學(xué)習(xí)吧2023-07-07mac安裝scrapy并創(chuàng)建項(xiàng)目的實(shí)例講解
今天小編就為大家分享一篇mac安裝scrapy并創(chuàng)建項(xiàng)目的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06python實(shí)現(xiàn)從字符串中找出字符1的位置以及個(gè)數(shù)的方法
這篇文章主要介紹了python實(shí)現(xiàn)從字符串中找出字符1的位置以及個(gè)數(shù)的方法,對(duì)于Python字符串操作的學(xué)習(xí)有一定的幫助與借鑒作用,需要的朋友可以參考下2014-08-08