I am trying to update a plot within my GTK window with FunkAnimation. I want to click a button to start updating the plot which gets its data from a txt file. The txt-file gets updated constantly. The intent is to plot a temperature profile. Here is the simplified code:
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()
So I guess, the problem is, that the create_plot function is only called once. The plot window is created in the gui, but it doesn't get updated. I couldn't find a solution for my problem. Adding `return any' as suggested here didn't work.
You can see a working example here, the code is at the bottom of the page.
I'm aware that i have to implement something like threading for the logging and updating at a later time, but i don't even get it to work without.
Any Tips? :)