在qt5中嵌入matplotlib

2022-08-28 14:24:46 字數 4050 閱讀 5689

from matplotlib.backends.backend_qt5agg import

figurecanvasqtagg as figurecanvas

from matplotlib.figure import

figure

import matplotlib.pyplot as plt

這一部分是參考官網上的歷程和其他人的部落格寫的,原理不太清楚

class plotcanvas(figurecanvas):#

通過繼承figurecanvas類,使得該類既是乙個pyqt5的qwidget,又是乙個matplotlib的figurecanvas,這是連線pyqt5與matplotlib的關鍵

def__init__(self, parent=none, width=5, height=4, dpi=100):

fig = figure(figsize=(width, height), dpi=dpi)#

建立乙個figure,注意:該figure為matplotlib下的figure,不是matplotlib.pyplot下面的figure

self.axes = fig.add_subplot(111)#

呼叫figure下面的add_subplot方法,類似於matplotlib.pyplot下面的subplot方法

figurecanvas.

__init__(self, fig)#

初始化父類

self.setparent(parent)

figurecanvas.setsizepolicy(self,

qsizepolicy.expanding,

qsizepolicy.expanding)

figurecanvas.updategeometry(self)

self.plot()

defplot(self):

data = [random.random() for i in range(25)]

ax = self.figure.add_subplot(111)

ax.plot(data, 'r-

')ax.set_title(

'pyqt matplotlib example')

self.draw()

import

sys

from pyqt5.qtwidgets import

from pyqt5.qtgui import

qicon

from matplotlib.backends.backend_qt5agg import

figurecanvasqtagg as figurecanvas

from matplotlib.figure import

figure

import

matplotlib.pyplot as plt

import

random

class

def__init__

(self):

super().

__init__

() self.left = 10self.top = 10self.title = '

pyqt5 matplotlib example - pythonspot.com

'self.width = 640self.height = 400self.initui()

definitui(self):

self.setwindowtitle(self.title)

self.setgeometry(self.left, self.top, self.width, self.height)

m = plotcanvas(self, width=5, height=4)

m.move(0,0)

button = qpushbutton('

pyqt5 button

', self)

button.settooltip(

'this s an example button')

button.move(500,0)

button.resize(140,100)

self.show()

class

plotcanvas(figurecanvas):

def__init__(self, parent=none, width=5, height=4, dpi=100):

fig = figure(figsize=(width, height), dpi=dpi)

self.axes = fig.add_subplot(111)

figurecanvas.

__init__

(self, fig)

self.setparent(parent)

figurecanvas.setsizepolicy(self,

qsizepolicy.expanding,

qsizepolicy.expanding)

figurecanvas.updategeometry(self)

self.plot()

defplot(self):

data = [random.random() for i in range(25)]

ax = self.figure.add_subplot(111)

ax.plot(data, 'r-

')ax.set_title(

'pyqt matplotlib example')

self.draw() if

__name__ == '

__main__

':

自己加的乙個小功能,每秒重新整理一次影象,並對資料進行插值

class

plotcanvas(figurecanvas):

def__init__(self, parent=none,width=5, height=4, dpi=100):

fig=figure(figsize=(width,height),dpi=dpi)

self.axes = fig.add_subplot(111)#

呼叫figure下面的add_subplot方法,類似於matplotlib.pyplot下面的subplot方法

figurecanvas.__init__

(self,fig)

self.setparent(parent)

figurecanvas.setsizepolicy(self,

qtwidgets.qsizepolicy.expanding,

qtwidgets.qsizepolicy.expanding)

figurecanvas.updategeometry(self)

self.test()

deftest(self):

self.init_plot()

#每秒更新一次影象

timer =qtcore.qtimer(self)

timer.timeout.connect(self.update_figure)

timer.start(1000)

definit_plot(self):

x=[1,2,3,4,5,6,7,8,9]

y=[23,21,32,13,3,132,13,3,1]

self.axes.plot(x, y)

defupdate_figure(self,x,y):

x=numpy.linspace(0,10,10)

y = [random.randint(0, 10) for i in range(10)]

xx=numpy.linspace(0,10)

f=interpolate.interp1d(x,y,'

quadratic

')#產生插值曲線的函式

yy=f(xx)

self.axes.cla()

self.axes.plot(x,y,'o

',xx,yy)

self.draw()

Qt5 嵌入式裝置QT5動態庫編譯,QT5移植

perl 5.8 or later python 2.7 or later c compiler supporting the c 98 standard sdk提供 編譯套裝 sudo apt get install build essential sudo apt get install git...

C 11 在 Qt 5 中的應用

c 11 現在已經是 c 標準,也就沒有理由不在新的應用中使用。qt 4.8 是第乙個支援 c 11 特性的 qt 版本,不過這裡,我們首先介紹的是,qt 5 中如何結合使用 c 11。至於 qt 4.8,我們會在後續文章中進行闡述。顯而易見的是,比起 qt 4.8,qt 5 利用了更多的 c 11...

在Qt5使用中文(vs環境)

如果是使用mingw版本的qt create,也就是使用gcc編譯器應該沒那麼多事吧。不過我還是用慣了vs呢。好了,廢話不多說,開始總結vs下亂碼的解決方案。vs2003 把原始碼存成 utf 8格式編碼就可以直接中文了。vs2005 08 10 13 1 qstring s1 qstringlit...