[英]matplotlib animation in wxpython panel
我正在努力将wxPython和matplotlib结合在一起。
我想将动画的matplotlib对象嵌入wxPanel中。 数据应在运行时添加。
我的模块代码:
(我无法获得正确的格式,请参阅http://pastebin.com/PU5QFEzG )
'''
a panel to display a given set of data in a wxframe as a heatmap, using pcolor
from the matplotlib
@author: me
'''
import wx
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas #todo: OW 26.10.15 needed?
class plotPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.figure = plt.Figure()
self.subplot = self.figure.add_subplot(111)
plt.title('test')
self.canvas = FigureCanvas(self, -1, self.figure) #ToDo: OW 26.10.15 Verstehen
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
self.dataSet = []
self.animator = animation.FuncAnimation(self.figure,self.anim, interval=1000)
def anim(self, a):
if(len(self.dataSet) == 0):
return 0
i = a % len(self.dataSet)
obj = self.subplot.pcolor(self.dataSet[i], cmap='RdBu')
return obj
def add_data(self, data):
self.dataSet.append(data)
#
# Code for a standalone test run
#
class TestFrame(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(1000,1000))
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Status Bar')
if __name__ == '__main__':
from numpy.random import rand #todo: OW 26.10.15 remove
app = wx.App(redirect=False)
frame = TestFrame(None, 'Debug Frame')
panel = plotPanel(frame)
frame.Show()
C = rand(10,10)
panel.add_data(C)
C = rand(10,10)
panel.add_data(C)
C = rand(10,10)
panel.add_data(C)
app.MainLoop()
我现在正在努力向图表添加更多详细信息,例如颜色条或标题。
如果我在self.subplot.title = 'test'
添加self.subplot.title = 'test'
,则得到“'str'对象没有属性'get_animated'”。 如果我尝试plt.title('test')
,它没有任何作用。 添加标题,颜色条或图例的正确方法是什么?
要将功能添加到嵌入式matplotlib图,您必须使用面向对象的matplotlib API。 matplotlib图由一个图形( matplotlib.figure.Figure
实例)组成,其中包含一个或多个轴( matplotlib.axes.Axes
实例)。 他们每个人依次包含Drawables
(线条,图像,散点图等)。
甲标题可以被添加(或修改)的Figure
或到每个Axes
与设置方法,如Figure.suptitle
或Axes.set_title
(或者,在你的情况下, self.figure.suptitle()
或self.subplot.set_title
) 。
颜色条有点棘手,因为它需要创建一个数据对象(可映射):我们只能在anim
函数中创建它。 另外,我们也不想创建许多colorbar
实例。 一旦创建,我们只需要更新它。 实现起来很容易:在构造函数中使用None
实例化self.colorbar
,然后在动画函数中对照None
进行检查:如果为None
,则创建颜色条,如果不是,则进行更新:
class plotPanel(wx.Panel):
def __init__(self, parent):
...
self.colorbar = None
def anim(self, a):
...
self.subplot.set_title("Frame %i" % a)
if self.colorbar is None:
self.colorbar = self.figure.colorbar(obj)
else:
self.colorbar.update_normal(obj)
self.colorbar.update_normal(obj)
return obj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.