【pyqt5學習】——graphicView顯示matplotlib圖像

目錄

一、導入模塊

二、自定義一個matplotlib窗口類Figure

三、利用QT_designer繪制窗口

?四、寫邏輯代碼

五、結果展示


一、導入模塊

import matplotlibmatplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from matplotlib import pyplotpyplot.rcParams['font.sans-serif'] = ['SimHei']
pyplot.rcParams['axes.unicode_minus'] = False

二、自定義一個matplotlib窗口類Figure

# 重寫一個matplotlib圖像繪制類
class MyFigure(FigureCanvasQTAgg):def __init__(self,width=5,height=4,dpi = 100):# 1、創建一個繪制窗口Figure對象self.fig = Figure(figsize=(width,height),dpi=dpi)# 2、在父類中激活Figure窗口,同時繼承父類屬性super(MyFigure, self).__init__(self.fig)# 這里就是繪制圖像、示例def plotSin(self,x,y):self.axes0 = self.fig.add_subplot(111)self.axes0.plot(x,y)

三、利用QT_designer繪制窗口

轉換為py文件

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'TDA_ShiftFlip.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, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(520, 396)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.graphicsView_2 = QtWidgets.QGraphicsView(self.centralwidget)self.graphicsView_2.setGeometry(QtCore.QRect(40, 70, 391, 221))self.graphicsView_2.setObjectName("graphicsView_2")MainWindow.setCentralWidget(self.centralwidget)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.translateMainWindow.setWindowTitle(_translate("MainWindow", "TDA_ShiftFlip"))if __name__ == "__main__":import sysapp = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow()ui = Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_())

?四、寫邏輯代碼

1、實例化matplotlib窗口,此時這個實例類似于一個部件

F1 = MyFigure(width=5, height=4, dpi=100)

2、將窗口分成多個窗口

F1.axes1 = F1.fig.add_subplot(221)# 表示將窗口分成2*2的布局

3、在窗口繪制圖像

		x = np.arange(0, 50)y = np.random.rand(50)F1.axes1.hist(y, bins=50)F1.axes1.plot(x, y)F1.axes1.bar(x, y)F1.axes1.set_title("hist")

4、(非必選)將窗口縮放成graphicView控件大小

width,height = self.graphicsView_2.width(),self.graphicsView_2.height()
F1.resize(width,height)

5、創建一個場景,將窗口放置到場景中,將場景放置到graphicsView控件

		self.scene = QGraphicsScene()  # 創建一個場景self.scene.addWidget(F1)  # 將圖形元素添加到場景中self.graphicsView_2.setScene(self.scene)  # 將創建添加到圖形視圖顯示窗口

邏輯全部代碼:

from matplotlibFigure import MyFigurefrom PyQt5 import QtCore, QtWidgets
from PyQt5.Qt import QThread, pyqtSignal, QIcon
from PyQt5.QtWidgets import QMessageBox, QGraphicsScene, QGraphicsPixmapItem, QGridLayout
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtGui import QIcon, QImage, QPixmap
from PyQt5.QtCore import Qtimport sys
import cv2
import numpy as np
from matplotlib import pyplot as pltclass TDASUILogic(QtWidgets.QMainWindow, TDASUI):def __init__(self):super(TDASUILogic, self).__init__()self.setupUi(self)self.plotother()def plotother(self):F1 = MyFigure(width=5, height=4, dpi=100)F1.axes1 = F1.fig.add_subplot(111)x = np.arange(0, 50)y = np.random.rand(50)F1.axes1.hist(y, bins=50)F1.axes1.plot(x, y)F1.axes1.bar(x, y)F1.axes1.set_title("hist")width,height = self.graphicsView_2.width(),self.graphicsView_2.height()F1.resize(width,height)self.scene = QGraphicsScene()  # 創建一個場景self.scene.addWidget(F1)  # 將圖形元素添加到場景中self.graphicsView_2.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.graphicsView_2.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.graphicsView_2.setScene(self.scene)  # 將創建添加到圖形視圖顯示窗口

五、結果展示

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/255534.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/255534.shtml
英文地址,請注明出處:http://en.pswp.cn/news/255534.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

happens-before規則

1)程序順序規則:一個線程中的每個操作,happens-before于該線程中的任意后續操作。2)監視器鎖規則:對一個鎖的解鎖,happens-before于隨后對這個鎖的加鎖。3)volatile變量規則:對一個v…

what is ssao

說到ssao 就要從ao說起,ao,即間接環境光遮蔽技術。我們知道現實中的光線,除了來自太陽和電燈的直射光線以外,光線碰到物體以后,還會再次反射,折射,而再次反射折射的過程中,又會被其他…

【pyqt5學習】——groupBox顯示matplotlib圖像

目錄 一、導入模塊 二、創建matplotlib窗口類 三、qt_designer設計窗口 四、邏輯代碼 五、結果展示 一、導入模塊 import matplotlibmatplotlib.use("Qt5Agg") from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg from matplotlib.figure impor…

[BZOJ3545][ONTAK2010]Peaks

[BZOJ3545][ONTAK2010]Peaks 試題描述 在Bytemountains有N座山峰,每座山峰有他的高度h_i。有些山峰之間有雙向道路相連,共M條路徑,每條路徑有一個困難值,這個值越大表示越難走,現在有Q組詢問,每組詢問詢問從…

杭電1027Ignatius and the Princess II模擬

地址:http://acm.hdu.edu.cn/showproblem.php?pid1027 題目: Problem DescriptionNow our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has t…

angular 使用rxjs 監聽同級兄弟組件數據變化

angular 的官網給出了父子組件之間數據交互的方法,如ViewChild、EventEmitter 但是如果要在同級組件之間進行數據同步,似乎并沒有給出太多的信息。 有時候我們想,在一個組件中修改數據之后,馬上反映到另外一個組件中, …

OpenCV里IplImage的widthStep參數 和width參數

一直以為IplImage結構體中的widthStep元素大小等于width*nChannels,大錯特錯!(為了快速訪問,要內存對齊啊)查看OpenCV2.1的源碼,在src/cxcore/cxarray.cpp文件中,找到cvInitImageHeader函數&…

【數字信號處理】——Python頻譜繪制

# -*- coding: utf-8 -*- from matplotlib import pyplotpyplot.rcParams[font.sans-serif] [SimHei] pyplot.rcParams[axes.unicode_minus] Falseimport numpy as np import matplotlib.pyplot as pl import matplotlib import math import randomN 500 # 繪制點總數 fs 5…

Android開發:《Gradle Recipes for Android》閱讀筆記1.3

想命令行執行gradle的構建,可以通過提供的gradle wrapper或者安裝gradle。 構建android項目不需要安裝gradle,因為android studio已經包含gradle。"gradle wrapper"指的是根目錄下的gradlew和gradlew.bat腳本(結尾的w是wrapper的意…

pic

轉載于:https://www.cnblogs.com/edisonxiang/p/5392651.html

leetcode 643 Maximum Average Subarray I

題目詳情 Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. 輸入一個數組nums和一個整數k。要求找出輸入數組中長度為k的子數組&#xff0c…

OpenCV之cvSmooth函數平滑濾波

1、cvSmooth函數用法 定義原型 <span style"font-size:12px;"> void cvSmooth( const CvArr* src, CvArr* dst,int smoothtypeCV_GAUSSIAN,int param1, int param2, double param3, double param4 );</span>src:輸入圖像. dst:輸出圖像. smoot…

【python數字信號處理】——DFT、DTFT(頻譜圖、幅度圖、相位圖)

目錄 一、離散時間傅里葉變換DTFT 二、離散傅里葉變換DFT 三、DFT與DTFT的關系 ? 參考&#xff1a; 《數字信號處理》——&#xff08;一&#xff09;.DTFT、DFT(python實現)_遠行者223的博客-CSDN博客python繪制頻譜圖DTFT&#xff0c;DFTpython繪制頻譜圖&#xff1a;…

ERROR:Tried to register widget id ==basemapGalleryDiv but that id is already registered解決辦法

在ArcGIS Server開發中&#xff0c;遇到DIV已經被注冊的情況&#xff0c;不能對原DIV內容進行更新。這里需要調用Dojo的destroyRecursive&#xff08;&#xff09;方法&#xff0c;逐個銷毀該Widget下的子元素及其后代元素。然后就可以在原DIV上注冊新的小部件。 示例代碼&…

通過Spring Data Neo4J操作您的圖形數據庫

在前面的一篇文章《圖形數據庫Neo4J簡介》中&#xff0c;我們已經對其內部所使用的各種機制進行了簡單地介紹。而在我們嘗試對Neo4J進行大版本升級時&#xff0c;我發現網絡上并沒有任何成型的樣例代碼以及簡介&#xff0c;而其自身的文檔也對如何使用Spring Data Neo4J介紹得語…

圖像金字塔

圖像金字塔被廣泛用于各種視覺應用中。圖像金字塔是一個圖像集合&#xff0c;集合中所有的圖像都源于同一個原始圖像&#xff0c;而且是通過對原始圖像連續降采樣活得&#xff0c;直到達到某個中止條件才停止降采樣。&#xff08;當然&#xff0c;降為一個像素肯定是中止條件。…

python使用git進行版本控制-分支管理

1、遠程克隆 最好的方式是先創建遠程庫&#xff0c;然后&#xff0c;從遠程庫克隆&#xff1a; 首先在github上創建一個新的倉庫&#xff0c;名字叫gitskills 我們勾選Initialize this repository with a README&#xff0c;這樣GitHub會自動為我們創建一個README.md文件。 下一…

【python數字信號處理】——Z變換

目錄 一、公式 二、代碼 三、結果 一、公式 頻域變量&#xff1a;z 時域變量&#xff1a;n 常見序列的Z變換&#xff1a;信號與系統復習歸納&#xff08;十一&#xff09;&#xff1a;Z變換例題_百把人的博客-CSDN博客_z變換例題基于東南大學陳從顏譯《信號、系統和變換》和…

九宮格拼圖 支持44 55等

代碼下載轉載于:https://www.cnblogs.com/ygcool/p/5395343.html

144. Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes values. For example:Given binary tree {1,#,2,3}, 1\2/3return [1,2,3]. 該題是對樹做前序遍歷 下面分別是遞歸&#xff0c;非遞歸&#xff0c;分治三種思路的解題結果 #遞歸寫法 class Solution(object):d…