gpt4 book ai didi

Python matplotlib 调用 savefig 后更新图形

转载 作者:太空宇宙 更新时间:2023-11-03 19:32:38 26 4
gpt4 key购买 nike

我的问题是:我在 PyGTK 应用程序中有 Matplotlib 图,它每隔几秒就会不断更新。我添加了将图形作为 PNG 文件保存到磁盘的功能。调用figure.savefig(文件名,其他参数)后,我在应用程序中的图形停止更新。

图初始化阶段:

# 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)

文件已保存,但屏幕上的图形停止更新

有什么想法如何将图形保存到磁盘并仍然能够在屏幕上更新我的图形?

最佳答案

您是否尝试过更新线条数据而不是重新创建图形?这假设数据点的数量不会改变每帧。它可能有助于解决拒绝更新的问题,并且至少会更快。

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()

关于Python matplotlib 调用 savefig 后更新图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5029686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com