如何使用python和Matplotlib更新绘图

如何使用python和Matplotlib更新绘图,python,wxpython,matplotlib,Python,Wxpython,Matplotlib,我一直在用python和wxpython使用matplotlib更新图表。我想按下一个按钮,将数据添加到wx.notebook中嵌套的图形中。下面是代码 谢谢你的帮助 import wx import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas class Plot(wx.Panel):

我一直在用python和wxpython使用matplotlib更新图表。我想按下一个按钮,将数据添加到wx.notebook中嵌套的图形中。下面是代码

谢谢你的帮助

import wx
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas

class Plot(wx.Panel):
    def __init__(self, parent, id = -1, dpi = None, **kwargs):
        wx.Panel.__init__(self, parent, id=id, **kwargs)
        self.figure = mpl.figure.Figure(dpi=dpi, figsize=(2,2))
        self.canvas = Canvas(self, -1, self.figure)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas,1,wx.EXPAND)
        self.SetSizer(sizer)

class JBC(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600,600))
        self.SetBackgroundColour(wx.Colour(236, 233, 216))

        self.nbG = wx.Notebook(self, -1, style=0, size=(400,400), pos=(0,0))       
        self.gSheet1 = self.add("Test").gca()

        calcButton = wx.Button(self, wx.NewId(), "Update", pos=(0, self.nbG.Position.y+400))

        #self.gSheet1.hold(False)
        #self.gSheet1.set_xlim(0,20)
        #self.gSheet1.set_ylim(0,20)
        #for i in range (2):
        #    self.gSheet1.plot([0,10],[1*i,1+i])

        #axes2 = plotter.add('figure 2').gca()
        #axes2.plot([1,2,3,4,5],[2,1,4,2,3])

        self.Bind(wx.EVT_BUTTON, self.OnCalculate, calcButton)

        self.Show(True)

    def OnCalculate(self, event):
        self.gSheet1.set_xlim(0,20)
        self.gSheet1.set_ylim(0,20)
        self.gSheet1.plot([1,2,3,4,5],[2,1,4,2,3])
        self.Update()

    def add(self,name="plot"):
       page = Plot(self.nbG)
       self.nbG.AddPage(page,name)
       return page.figure

    def Update(self):
        self.gSheet1.clear()
        plt.draw()
        print "Tried to redraw"


app = wx.App()
JBC(None, -1, "Test Title")
app.MainLoop()
作为指导,可以尝试以下方法:

import wx
import matplotlib as mpl
mpl.use('WXAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas

class Plot(wx.Panel):
    def __init__(self, parent, id = -1, dpi = None, **kwargs):
        wx.Panel.__init__(self, parent, id=id, **kwargs)
        self.figure = mpl.figure.Figure(dpi=dpi, figsize=(2,2))
        self.canvas = Canvas(self, -1, self.figure)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas,1,wx.EXPAND)
        self.SetSizer(sizer)

class JBC(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600,600))
        self.SetBackgroundColour(wx.Colour(236, 233, 216))

        self.nbG = wx.Notebook(self, -1, style=0, size=(400,400), pos=(0,0))       
        self.gSheet1 = self.add("Test").gca()

        calcButton = wx.Button(self, wx.NewId(), "Update", pos=(0, self.nbG.Position.y+400))

        #self.gSheet1.hold(False)
        #self.gSheet1.set_xlim(0,20)
        #self.gSheet1.set_ylim(0,20)
        #for i in range (2):
        #    self.gSheet1.plot([0,10],[1*i,1+i])

        #axes2 = plotter.add('figure 2').gca()
        #axes2.plot([1,2,3,4,5],[2,1,4,2,3])

        self.Bind(wx.EVT_BUTTON, self.OnCalculate, calcButton)        
        # self.Show(True)

    def OnCalculate(self, event):
        self.gSheet1.set_xlim(0,20)
        self.gSheet1.set_ylim(0,20)
        self.gSheet1.plot([1,2,3,4,5],[2,1,4,2,3])
        self.Update()

    def add(self,name="plot"):
       page = Plot(self.nbG)
       self.nbG.AddPage(page,name)
       return page.figure

    def Update(self):
        self.gSheet1.clear()
        plt.draw()
        print "Tried to redraw"

if __name__ == '__main__':
    app = wx.App()
    frame=JBC(None, -1, "Test Title")
    frame.Show()
    app.MainLoop()
也可以使用matplotlib绘制动画图形:

"""
Based on Tkinter bouncing ball code:
http://stackoverflow.com/q/13660042/190597 (arynaq) and
http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/
"""

import wx
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.figure as mplfig
import scipy.spatial.distance as dist
import matplotlib.backends.backend_wxagg as mwx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, size = (800, 600))
        self.panel = wx.Panel(self)        
        self.fig = mplfig.Figure(figsize = (5, 4), dpi = 100)
        self.ax = self.fig.add_subplot(111)
        self.vbox = wx.BoxSizer(wx.VERTICAL)        
        self.canvas = mwx.FigureCanvasWxAgg(self.panel, wx.ID_ANY, self.fig)
        self.toolbar = mwx.NavigationToolbar2WxAgg(self.canvas)
        self.button = wx.Button(self.panel, wx.ID_ANY, "Quit")
        self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.vbox.Add(self.toolbar, 0, wx.EXPAND)        
        self.vbox.Add(
            self.button, 0, border = 3,
            flag = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL)
        self.panel.SetSizer(self.vbox)
        self.vbox.Fit(self)
        self.toolbar.update()
        self.update = self.animate().next
        self.timer = wx.Timer(self)
        self.timer.Start(1)
        self.Bind(wx.EVT_BUTTON, self.OnCloseWindow, self.button)        
        self.Bind(wx.EVT_TIMER, lambda event: self.update())
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def OnCloseWindow(self, evt):
        self.timer.Stop()
        del self.timer
        self.Destroy()

    def animate(self):
        N = 100                                             #Number of particles
        R = 10000                                           #Box width
        pR = 5                                               #Particle radius

        r = np.random.randint(0, R, (N, 2))                  #Position vector
        v = np.random.randint(-R/100, R/100, (N, 2))           #velocity vector
        a = np.array([0, -10])                               #Forces
        v_limit = R/2                                       #Speedlimit

        line, = self.ax.plot([], 'o')
        line2, = self.ax.plot([], 'o')                           #Track a particle
        self.ax.set_xlim(0, R+pR)
        self.ax.set_ylim(0, R+pR)        

        while True:
            v = v+a                                           #Advance
            r = r+v

            #Collision tests
            r_hit_x0 = np.where(r[:, 0]<0)                   #Hit floor?
            r_hit_x1 = np.where(r[:, 0]>R)                   #Hit roof?
            r_hit_LR = np.where(r[:, 1]<0)                   #Left wall?
            r_hit_RR = np.where(r[:, 1]>R)                   #Right wall?

            #Stop at walls
            r[r_hit_x0, 0] = 0
            r[r_hit_x1, 0] = R
            r[r_hit_LR, 1] = 0
            r[r_hit_RR, 1] = R

            #Reverse velocities
            v[r_hit_x0, 0] = -0.9*v[r_hit_x0, 0]
            v[r_hit_x1, 0] = -v[r_hit_x1, 0]
            v[r_hit_LR, 1] = -0.95*v[r_hit_LR, 1]
            v[r_hit_RR, 1] = -0.99*v[r_hit_RR, 1]

            #Collisions
            D = dist.squareform(dist.pdist(r))
            ind1, ind2 = np.where(D < pR)
            unique = (ind1 < ind2)
            ind1 = ind1[unique]
            ind2 = ind2[unique]

            for i1, i2 in zip(ind1, ind2):
                eps = np.random.rand()
                vtot = v[i1, :]+v[i2, :]
                v[i1, :] = -(1-eps)*vtot
                v[i2, :] = -eps*vtot

            line.set_ydata(r[:, 1])
            line.set_xdata(r[:, 0])
            line2.set_ydata(r[:N/5, 1])
            line2.set_xdata(r[:N/5, 0])
            self.canvas.draw()
            yield True

def main():
    app = wx.App(False)
    frame = Frame()
    frame.Show(True)
    app.MainLoop()

if __name__ == '__main__':
    main()
“”“
基于Tkinter弹跳球代码:
http://stackoverflow.com/q/13660042/190597 (arynaq)和
http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/
"""
导入wx
将numpy作为np导入
将matplotlib.pyplot作为plt导入
将matplotlib.figure导入为mplfig
将scipy.spatial.distance导入为dist
将matplotlib.backends.backend_wxagg作为mwx导入
类框架(wx.Frame):
定义初始化(自):
wx.Frame.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.panel=wx.panel(self)
self.fig=mplfig.Figure(figsize=(5,4),dpi=100)
self.ax=self.fig.add_子批次(111)
self.vbox=wx.BoxSizer(wx.VERTICAL)
self.canvas=mwx.FigureCanvasWxAgg(self.panel,wx.ID_ANY,self.fig)
self.toolbar=mwx.NavigationToolbar2WxAgg(self.canvas)
self.button=wx.button(self.panel,wx.ID\u ANY,“退出”)
self.vbox.Add(self.canvas,1,wx.LEFT | wx.TOP | wx.GROW)
self.vbox.Add(self.toolbar,0,wx.EXPAND)
self.vbox.Add(
self.button,0,border=3,
flag=wx.ALIGN|u LEFT | wx.ALL | wx.ALIGN|u CENTER|u VERTICAL)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
self.toolbar.update()
self.update=self.animate()。下一步
self.timer=wx.timer(self)
自动定时器启动(1)
self.Bind(wx.EVT_按钮、self.OnCloseWindow、self.BUTTON)
self.Bind(wx.EVT_计时器,lambda事件:self.update())
self.Bind(wx.EVT\u CLOSE,self.OnCloseWindow)
def OnCloseWindow(自我,evt):
self.timer.Stop()
自动定时器
自我毁灭
def动画(自):
N=100#粒子数
R=10000#箱宽
pR=5#粒子半径
r=np.random.randint(0,r,(N,2))#位置向量
v=np.random.randint(-R/100,R/100,(N,2))#速度矢量
a=np.数组([0,-10])#力
v#U限制=R/2#速度限制
行,=self.ax.plot([],'o')
line2,=self.ax.plot([],'o')#跟踪粒子
自身最大设置(0,R+pR)
自最大值集_ylim(0,R+pR)
尽管如此:
v=v+a#前进
r=r+v
#碰撞试验
r_hit_x0=np.其中(r[:,0]r)#击中屋顶?
r_hit_LR=np.哪里(r[:,1]r)#右墙?
#停在墙边
r[r\u hit\u x0,0]=0
r[r_hit_x1,0]=r
r[r_hit_LR,1]=0
r[r_hit_RR,1]=r
#反向速度
v[r\u hit\u x0,0]=-0.9*v[r\u hit\u x0,0]
v[r_hit_x1,0]=-v[r_hit_x1,0]
v[r_hit_LR,1]=-0.95*v[r_hit_LR,1]
v[r_hit_RR,1]=-0.99*v[r_hit_RR,1]
#碰撞
D=距离方形(距离pdist(r))
ind1,ind2=np,其中(D
此处是否不能使用matplotlib动态打印,而不是使用更新按钮?@BhoomikaSheth:是的,请参见示例。