文章正文
matplotlib与panel结合的动态图
下面的例子显示了python动态图的做法,可以适用于其他图标,例如柱状图等。
#encoding=utf8
import matplotlib
import wx
import numpy as np
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import threading
import time
class CpuInfoWindow(wx.Frame):
def __init__(self):
super(CpuInfoWindow,self).__init__(None,title='test',size=(600,600))
self.j = 0
#panel
self.panel = wx.Panel(self)
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self,-1,self.figure)
self.axes.set_xlim(0,10)
self.axes.set_ylim(-2,2)
#ready all things
#self.bg = self.canvas.copy_from_bbox(self.axes.bbox)
self.x = np.arange(0,3.0,0.01)
self.y = np.sin(2*np.pi*self.x)
self.fg, = self.axes.plot(self.x,self.y)
print(dir(self.axes))
#button
timer = threading.Timer(1,self.draw)
timer.start()
def draw(self):
#panel
self.j=self.j+1
#self.canvas.restore_region(self.bg)
self.axes.set_xlim(0+0.1*self.j,10+0.1*self.j)
self.axes.set_ylim(-2,2)
self.x = np.arange(0+0.1*self.j,3.0+0.1*self.j,0.01)
self.y = np.sin(2*np.pi*self.x)
self.fg.set_xdata(self.x)
self.fg.set_ydata(self.y)
self.canvas.draw()
timer = threading.Timer(0.05,self.draw)
timer.start()
if __name__=="__main__":
win = wx.App()
cpu = CpuInfoWindow()
cpu.Show()
win.MainLoop()后记:Mac 安装matplotlib和wxpython后,出现一个小问题,Google之后解决
ImportError: No module named wxversion
解决方法:On OS X do not ever install python modules as root
python -m pip install --user --upgrade matplotlib
然后用IDLE启动python脚本
Feb. 22, 2018, 8:39 p.m. 作者:zachary 分类:python小站 阅读(3218) 评论(0)
评论列表:
评论: