我的问题是:
我在PyGTK应用程序中有Matplotlib图,每隔几秒就会进行一次更新。我添加了将数据保存为磁盘的能力,如PNG文件。调用figure.savefig(filename, other parameters)
后,我的应用程序中的数字停止更新。
图初始化阶段:
# setup matplotlib stuff on empty space in vbox4
figure = Figure()
canvas = FigureCanvasGTK(figure) # a gtk.DrawingArea
canvas.show()
self.win.get_widget('vbox4').pack_start(canvas, True, True) # this will be aded to last place
self.win.get_widget('vbox4').reorder_child(canvas, 1) #place plot to space where it should be
图正在以这种方式更新(这在单独的线程中每隔几秒调用一次):
def _updateGraph(self, fig, x, x1, y):
#Various calculations done here
fig.clf()#repaint plot: delete current and formate a new one
axis = fig.add_subplot(111)
#axis.set_axis_off()
axis.grid(True)
#remove ticks and labels
axis.get_xaxis().set_ticks_position("none")
for i in range(len(axis.get_xticklabels())): axis.get_xticklabels()[i].set_visible(False)
axis.get_yaxis().set_ticks_position("none")
axis.plot(numpy.array(x),numpy.array(y)/(1.0**1), "k-" ,alpha=.2)
axis.set_title('myTitle')
fig.autofmt_xdate()
fig.canvas.draw()
一切都按预期工作。但是在打电话之后:
figure.savefig(fileName, bbox_inches='tight', pad_inches=0.05)
文件已保存,但屏幕上的数字不再更新。
任何想法如何将数字保存到磁盘并仍然可以在屏幕上更新我的图形?
答案 0 :(得分:1)
您是否尝试过更新行数据而不是重新创建数字?这假设数据点的数量不会改变每一帧。它可能有助于解决拒绝更新的问题,至少它会更快。
def _updateGraph(self, fig, x, x1, y):
#Various calculations done here
ydata = numpy.array(y)/(1.0**1)
# retrieved the saved line object
line = getattr(fig, 'animated_line', None);
if line is None:
# no line object so create the subplot and axis and all
fig.clf()
axis = fig.add_subplot(111)
axis.grid(True)
#remove ticks and labels
axis.get_xaxis().set_ticks_position("none")
for i in range(len(axis.get_xticklabels())):
axis.get_xticklabels()[i].set_visible(False)
axis.get_yaxis().set_ticks_position("none")
xdata = numpy.array(x);
line = axis.plot(xdata, ydata, "k-" ,alpha=.2)
axis.set_title('myTitle')
fig.autofmt_xdate()
# save the line for later reuse
fig.animated_line = line
else:
line.set_ydata(ydata)
fig.canvas.draw()
答案 1 :(得分:0)
我找到了一个圆满的工作。因为我的数字在调用figure.savefig()
之后拒绝更新,所以我找到了一种方法来解决它。我的数字在HBox2容器内(GUI是用Glade 3.6.7创建的)作为第一个元素
# some stuff going
figure.saveFig(fileName)
# WORK-A-ROUND: delete figure after calling savefig()
box = self.win.get_widget('hbox2')
box.remove(box.get_children()[0])
self._figPrepare()
def _figPrepare(self): #initialize graph
figure = Figure()
canvas = FigureCanvasGTK(figure) # a gtk.DrawingArea
canvas.show()
figure.clf()
gui.w().set("figure", figure)
self.win.get_widget('hbox2').pack_start(canvas, True, True) # this will be aded to last place
self.win.get_widget('hbox2').reorder_child(canvas, 0) #place plot to space where it should be
我知道这不是最佳做法,可能很慢,但对我来说还可以。希望别人能找到这个有用的
答案 2 :(得分:0)
来自http://matplotlib.org/examples/user_interfaces/embedding_in_gtk2.html
似乎有帮助的是“agg”不确定这意味着什么,但为我修复了这个错误:)
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas