python Matplotlib gtk - 使用 FuncAnimation 制作动画图
python Matplotlib gtk - animate plot with FuncAnimation
我正在尝试使用 FunkAnimation 更新我的 GTK window 中的情节。
我想单击一个按钮开始更新从 txt 文件获取数据的绘图。 txt 文件不断更新。目的是绘制温度曲线。这是简化的代码:
import gtk
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
import matplotlib.animation as animation
from matplotlib import pyplot as plt
class MyProgram():
def __init__(self):
some_gtk_stuff
self.signals = {
'on_button_clicked': self.create_plot,
}
self.builder.connect_signals(self.signals)
self.vbox = self.builder.get_object('vbox')
self.figure = plt.figure()
self.axis = self.figure.add_subplot(1,1,1)
self.init = 0
def create_plot(self):
def update_plot(i):
#read SampleData from txt File
x = []
y = []
readFile = open('SampleData.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()
for data in sepFile:
xy = data.split(',')
x.append(int(x)
y.append(int(y)
self.axis.plot(x, y)
if (self.init == 0):
self.canvas = FigureCanvas(self.figure)
self.vbox.pack_start(self.canvas)
self.canvas.show()
ani = animation.FuncAnimation(self.figure, update_plot, interval = 1000)
self.canvas.draw()
return ani
MyProgram()
gtk.main()
所以我想,问题是 create_plot 函数只被调用了一次。绘图 window 是在 gui 中创建的,但不会更新。我找不到解决我的问题的方法。按照建议 here 添加“return any”无效。
你可以看到一个工作示例here,代码在页面底部。
我知道我必须为以后的日志记录和更新实现线程之类的东西,但如果没有它我什至无法正常工作。
有什么建议吗? :)
我想这就是你想要达到的目的。请注意,我改变了您阅读文件的方式。 with open() as f:
负责您忘记的文件关闭操作。也可以在构建器文件中写入信号处理程序的名称,这样就可以简单地说 self.builder.connect(self)
并省略 self.signals
字典。
import gtk
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
import matplotlib.animation as animation
from matplotlib import pyplot as plt
class MyProgram():
def __init__(self):
#some_gtk_stuff
self.signals = {
'on_button_clicked': self.create_plot,
}
self.builder.connect_signals(self.signals)
self.vbox = self.builder.get_object('vbox')
self.figure = plt.figure()
self.axis = self.figure.add_subplot(1,1,1)
self.canvas = None
def create_plot(self, button):
self.ani = animation.FuncAnimation(self.figure, self.update_plot, interval = 1000)
def update_plot(self, i):
#read SampleData from txt File
x = []
y = []
with open('SampleData.txt') as f:
x_raw, y_raw = f.readline().strip().split(',')
x.append(int(x_raw))
y.append(int(y_raw))
self.axis.plot(x, y)
if not self.canvas:
self.canvas = FigureCanvas(self.figure)
self.vbox.pack_start(self.canvas)
self.canvas.show()
self.canvas.draw()
MyProgram()
gtk.main()
我正在尝试使用 FunkAnimation 更新我的 GTK window 中的情节。 我想单击一个按钮开始更新从 txt 文件获取数据的绘图。 txt 文件不断更新。目的是绘制温度曲线。这是简化的代码:
import gtk
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
import matplotlib.animation as animation
from matplotlib import pyplot as plt
class MyProgram():
def __init__(self):
some_gtk_stuff
self.signals = {
'on_button_clicked': self.create_plot,
}
self.builder.connect_signals(self.signals)
self.vbox = self.builder.get_object('vbox')
self.figure = plt.figure()
self.axis = self.figure.add_subplot(1,1,1)
self.init = 0
def create_plot(self):
def update_plot(i):
#read SampleData from txt File
x = []
y = []
readFile = open('SampleData.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()
for data in sepFile:
xy = data.split(',')
x.append(int(x)
y.append(int(y)
self.axis.plot(x, y)
if (self.init == 0):
self.canvas = FigureCanvas(self.figure)
self.vbox.pack_start(self.canvas)
self.canvas.show()
ani = animation.FuncAnimation(self.figure, update_plot, interval = 1000)
self.canvas.draw()
return ani
MyProgram()
gtk.main()
所以我想,问题是 create_plot 函数只被调用了一次。绘图 window 是在 gui 中创建的,但不会更新。我找不到解决我的问题的方法。按照建议 here 添加“return any”无效。
你可以看到一个工作示例here,代码在页面底部。
我知道我必须为以后的日志记录和更新实现线程之类的东西,但如果没有它我什至无法正常工作。
有什么建议吗? :)
我想这就是你想要达到的目的。请注意,我改变了您阅读文件的方式。 with open() as f:
负责您忘记的文件关闭操作。也可以在构建器文件中写入信号处理程序的名称,这样就可以简单地说 self.builder.connect(self)
并省略 self.signals
字典。
import gtk
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
import matplotlib.animation as animation
from matplotlib import pyplot as plt
class MyProgram():
def __init__(self):
#some_gtk_stuff
self.signals = {
'on_button_clicked': self.create_plot,
}
self.builder.connect_signals(self.signals)
self.vbox = self.builder.get_object('vbox')
self.figure = plt.figure()
self.axis = self.figure.add_subplot(1,1,1)
self.canvas = None
def create_plot(self, button):
self.ani = animation.FuncAnimation(self.figure, self.update_plot, interval = 1000)
def update_plot(self, i):
#read SampleData from txt File
x = []
y = []
with open('SampleData.txt') as f:
x_raw, y_raw = f.readline().strip().split(',')
x.append(int(x_raw))
y.append(int(y_raw))
self.axis.plot(x, y)
if not self.canvas:
self.canvas = FigureCanvas(self.figure)
self.vbox.pack_start(self.canvas)
self.canvas.show()
self.canvas.draw()
MyProgram()
gtk.main()