Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib.animation - AttributeError: 'NoneType' object has no attribute 'new_timer'

Tags:

The code below is a snippet of the code i have written, It basically shows a graph in a tkinter window but then struggles when it comes to adding animation(updating) the graph. The following is the error i keep getting, any help would be much appreciated.

Traceback (most recent call last):
  File "C:\Users\nasto\Documents\Company\CMSFRAME.py", line 163, in <module>
    ani = animation.FuncAnimation (f, animate, interval=1000)
  File "C:\Users\nasto\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1462, in __init__
    TimedAnimation.__init__(self, fig, **kwargs)
  File "C:\Users\nasto\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1225, in __init__
    event_source = fig.canvas.new_timer()
AttributeError: 'NoneType' object has no attribute 'new_timer'

The snippet of my code:

      canvas =  FigureCanvasTkAgg(f, self)
      canvas.show()
      canvas.get_tk_widget().pack(side=tk.LEFT,  fill=tk.BOTH, expand = True)
      toolbar = NavigationToolbar2TkAgg(canvas, self)
      toolbar.update()
      canvas._tkcanvas.pack(side=tk.LEFT,  fill=tk.BOTH, expand = True)

f = Figure(figsize = (5,5), dpi=100)
a = f.add_subplot(111)

def  animate(interval):
    pullData = open('financeData.txt','r').read()
    dataList = pullData.split('\n')
    xList = []
    yList=[]
    for eachLine in dataList:
        if len(eachline) >1:
            x,  y = eachLine.split(',')
            xList.append(int(x))
            yList.append(int(x))
    a.clear()
    a.plot(xList, yList)

app = start()
ani = animation.FuncAnimation (f, animate, interval=1000)
like image 335
Naseem Tomkinson Avatar asked Mar 22 '17 23:03

Naseem Tomkinson


1 Answers

The error basically tells you that the figure does not have a canvas.

To prevent that you need to make sure to only start the animation once the figure is added to the FigureCanvasTkAgg. Since we lack the complete code in the question, I can only link you to some other complete examples that are reported to work:

  1. Embedding matplotlib canvas into tkinter GUI - plot is not showing up, but no error is thrown
  2. Python. Error using animation.FuncAnimation

In the second case the code from the question produces exactly the same error and a possible solution is to incorporate the animation in the tk class.

like image 140
ImportanceOfBeingErnest Avatar answered Sep 22 '22 10:09

ImportanceOfBeingErnest