【发布时间】:2015-09-13 02:57:29
【问题描述】:
我已经实现了一个sync block,它使用input_items 值在其work 函数内绘图。现在的问题是绘图机制对于输入流来说不够快(input_items 的值不断变化)。
我已尝试尽可能简化代码并添加了 cmets。这里是:
....
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
temp = ''
class xyz(gr.sync_block):
def __init__(self,parent,title,order):
a = []
self.count = 1
gr.sync_block.__init__(self,
name="xyz",
in_sig=[numpy.float32,numpy.float32],
out_sig=None)
self.win = xyzPlot(parent) #I have a Top Block(wxFrame). I am just making it the parent of xyzPlot(wxPanel) here.
def work(self, input_items, output_items):
global temp
if (self.count == 1):
temp = input_items+list()
bool = not(np.allclose(temp,input_items))#bool will be true if the value of `input_items` changes.
.......
#the code that evaluates z1,z2 depending on the value of input_items
.......
if ( bool or self.count == 1 ):
#If bool is true or it is the first time then I am calling plot() which plots the graph.
self.win.plot(tf(self.z1,self.z3),None,True,True,True,True)
self.count = 0
temp = input_items+list()
return len(input_items[0])
class xyzPlot(wx.Panel):
def __init__(self, parent, dB=None, Hz=None, deg=None):
wx.Panel.__init__(self , parent , -1 ,size=(600,475))
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
def plot(self, syslist, omega=None, dB=None, Hz=None, deg=None, Plot=True, *args , **kwargs):
self.axes.clear() #I clear the graph here so that new values can be plotted.
.....
self.axes.semilogx(omega,mag,*args,**kwargs)
self.canvas = FigCanvas(self, -1, self.fig)
self.canvas.draw()
如您所见,我正在使用 wxPython,但是每当我将 input_items 的值更改得太快时,面板就会冻结(如果我慢慢更改它就可以正常工作)。有什么建议吗?我是新手。
【问题讨论】:
-
不要清除轴并重建艺术家。创建一次,并保留对它的引用。然后您可以使用
set_data仅更改数据点。同样适用于画布。只创建一次,然后调用draw应该足以更新图形。 -
@hitzg:是的,但问题似乎是他在非 GUI 线程的线程中异步且高速地绘制事物。我认为您可能更习惯于 matplotlib/wx 集成,所以我很乐意在我的 answer 下看到您的评论。
-
@MarcusMüllerꕺꕺ 不是真的。我的评论只是关于如何加快剧情更新的建议。
-
@hitzg:我理解画布的变化。在他使用
semilogx的情况下如何使用set_data? -
semilogx与plot完全相同。它返回一个艺术家列表。
标签: python matplotlib wxpython gnuradio