matplotlib繫結到PyQt5(有選單)

2021-09-06 21:41:51 字數 4502 閱讀 7569

稍微複雜地實現matplotlib繫結到pyqt5(有選單)

【知識點】

import

matplotlib

matplotlib.use(

"qt5agg

")

【效果圖】

【源**】

1import

sys2

import

random34

import

matplotlib

5 matplotlib.use("

qt5agg")

67from pyqt5 import

qtcore

8from pyqt5.qtwidgets import910

from numpy import

arange, sin, pi

11from matplotlib.backends.backend_qt5agg import

figurecanvasqtagg as figurecanvas

12from matplotlib.figure import

figure

1314

15class

mymplcanvas(figurecanvas):

16"""

這是乙個視窗部件,即qwidget(當然也是figurecanvasagg)

"""17

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

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

19 self.axes = fig.add_subplot(111)20#

每次plot()呼叫的時候,我們希望原來的座標軸被清除(所以false)

21self.axes.hold(false)

2223

self.compute_initial_figure()

2425

#26 figurecanvas.__init__

(self, fig)

27self.setparent(parent)

2829

figurecanvas.setsizepolicy(self,

30qsizepolicy.expanding,

31qsizepolicy.expanding)

32figurecanvas.updategeometry(self)

3334

defcompute_initial_figure(self):

35pass

3637

class

mystaticmplcanvas(mymplcanvas):

38"""

靜態畫布:一條正弦線

"""39

defcompute_initial_figure(self):

40 t = arange(0.0, 3.0, 0.01)

41 s = sin(2*pi*t)

42self.axes.plot(t, s)

4344

45class

mydynamicmplcanvas(mymplcanvas):

46"""

動態畫布:每秒自動更新,更換一條折線。

"""47

def__init__(self, *args, **kwargs):

48 mymplcanvas.__init__(self, *args, **kwargs)

49 timer =qtcore.qtimer(self)

50timer.timeout.connect(self.update_figure)

51 timer.start(1000)

5253

defcompute_initial_figure(self):

54 self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r'

)5556def

update_figure(self):57#

構建4個隨機整數,位於閉區間[0, 10]

58 l = [random.randint(0, 10) for i in range(4)]

5960 self.axes.plot([0, 1, 2, 3], l, 'r'

)61self.draw()

6263

class

64def

__init__

(self):

65 qmainwindow.__init__

(self)

66self.setattribute(qtcore.qt.wa_deleteonclose)

67 self.setwindowtitle("

程式主視窗")

6869 self.file_menu = qmenu('

&file

', self)

70 self.file_menu.addaction('

&quit

', self.filequit,

71 qtcore.qt.ctrl +qtcore.qt.key_q)

72self.menubar().addmenu(self.file_menu)

7374 self.help_menu = qmenu('

&help

', self)

75self.menubar().addseparator()

76self.menubar().addmenu(self.help_menu)

7778 self.help_menu.addaction('

&about

', self.about)

7980 self.main_widget =qwidget(self)

8182 l =qvboxlayout(self.main_widget)

83 sc = mystaticmplcanvas(self.main_widget, width=5, height=4, dpi=100)

84 dc = mydynamicmplcanvas(self.main_widget, width=5, height=4, dpi=100)

85l.addwidget(sc)

86l.addwidget(dc)

8788

self.main_widget.setfocus()

89self.setcentralwidget(self.main_widget)90#

狀態條顯示2秒

91 self.statusbar().showmessage("

matplotlib 萬歲!

", 2000)

9293

deffilequit(self):

94self.close()

9596

defcloseevent(self, ce):

97self.filequit()

9899

defabout(self):

100 qmessagebox.about(self, "

about",

101"""

embedding_in_qt5.py example

102103

104105

canvases. it is base on example from matplolib documentation, and initially was

106developed from florent rougon and darren dale.

107108

109110

it may be used and modified with no restriction; raw copies as well as

111modified versions may be distributed without limitation.

112"""

113)

114115

if__name__ == '

__main__':

117119 aw.setwindowtitle("

pyqt5 與 matplotlib 例子")

120aw.show()

121#

matplotlib繫結到PyQt5(有選單)

稍微複雜地實現matplotlib繫結到pyqt5 有選單 知識點 import matplotlib matplotlib.use qt5agg 效果圖 源 1import sys2 import random34 import matplotlib 5 matplotlib.use qt5agg...

將py檔案打包到docx

import osclass filedownload def init self self.exclude db.sqlite3 logs media pipfile pipfile.lock readme.md 不需要打包的檔案 self.file list defget file self,p...

centos編譯python3繫結的pycaffe

寫在開頭 目前python3版本的pycaffe仍然屬於不穩定版本,有很多未知因素,所以還是非常建議使用python2.7編譯pycaffe。1.python3 centos預設不自帶python3,在此處安裝python3環節中強烈建議使用因為據google結果顯示,其他版本最後編譯pycaffe...