How to import a matplotlib 3D animation in a tkinter frame in python?












0














I'm trying to import a matplotlib 3D animation inside a Tkinter window but I got stuck on an error. My Tkinter window has many frames and I try to plot the animation on one of this frame. Finally, I create all elements inside a class init function. So here is a simplified version of my code:



from tkinter import *
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as anim
import random

class UI:
def __init__(self):
self.back = Frame(master=self.window, width=1200, height=800, bg='white')
self.rightBack = Frame(master=self.back, width=300, height=800)
self.rightBack.pack(side='right',padx=2,pady=2)
self.fig = Figure()
self.axes = Axes3D(self.fig)

self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False) # Creating animation

self.canvas = FigureCanvasTkAgg(self.fig, master=self.displayBack)
self.canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=True)


# Starting application
self.window.mainloop()

def updateDisplay(self,num):
self.axes.clear()
self.axes.plot([0,1,random.random()],[0,1,random.random()],[0,1,random.random()],'o',color='k',markersize='5')


But when I run the following in python



self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False)


I get this error



File "matplotlib/animation.py", line 1703, in __init__
TimedAnimation.__init__(self, fig, **kwargs)

File "matplotlib/animation.py", line 1465, in __init__
event_source = fig.canvas.new_timer()

AttributeError: 'NoneType' object has no attribute 'new_timer'


So I think I understood the error as animation needs a timer but I don't know how to fix it. Any help? Thank's in advance










share|improve this question





























    0














    I'm trying to import a matplotlib 3D animation inside a Tkinter window but I got stuck on an error. My Tkinter window has many frames and I try to plot the animation on one of this frame. Finally, I create all elements inside a class init function. So here is a simplified version of my code:



    from tkinter import *
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from matplotlib.figure import Figure
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.animation as anim
    import random

    class UI:
    def __init__(self):
    self.back = Frame(master=self.window, width=1200, height=800, bg='white')
    self.rightBack = Frame(master=self.back, width=300, height=800)
    self.rightBack.pack(side='right',padx=2,pady=2)
    self.fig = Figure()
    self.axes = Axes3D(self.fig)

    self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False) # Creating animation

    self.canvas = FigureCanvasTkAgg(self.fig, master=self.displayBack)
    self.canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=True)


    # Starting application
    self.window.mainloop()

    def updateDisplay(self,num):
    self.axes.clear()
    self.axes.plot([0,1,random.random()],[0,1,random.random()],[0,1,random.random()],'o',color='k',markersize='5')


    But when I run the following in python



    self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False)


    I get this error



    File "matplotlib/animation.py", line 1703, in __init__
    TimedAnimation.__init__(self, fig, **kwargs)

    File "matplotlib/animation.py", line 1465, in __init__
    event_source = fig.canvas.new_timer()

    AttributeError: 'NoneType' object has no attribute 'new_timer'


    So I think I understood the error as animation needs a timer but I don't know how to fix it. Any help? Thank's in advance










    share|improve this question



























      0












      0








      0







      I'm trying to import a matplotlib 3D animation inside a Tkinter window but I got stuck on an error. My Tkinter window has many frames and I try to plot the animation on one of this frame. Finally, I create all elements inside a class init function. So here is a simplified version of my code:



      from tkinter import *
      import matplotlib
      matplotlib.use("TkAgg")
      from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
      from matplotlib.figure import Figure
      from mpl_toolkits.mplot3d import Axes3D
      import matplotlib.animation as anim
      import random

      class UI:
      def __init__(self):
      self.back = Frame(master=self.window, width=1200, height=800, bg='white')
      self.rightBack = Frame(master=self.back, width=300, height=800)
      self.rightBack.pack(side='right',padx=2,pady=2)
      self.fig = Figure()
      self.axes = Axes3D(self.fig)

      self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False) # Creating animation

      self.canvas = FigureCanvasTkAgg(self.fig, master=self.displayBack)
      self.canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=True)


      # Starting application
      self.window.mainloop()

      def updateDisplay(self,num):
      self.axes.clear()
      self.axes.plot([0,1,random.random()],[0,1,random.random()],[0,1,random.random()],'o',color='k',markersize='5')


      But when I run the following in python



      self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False)


      I get this error



      File "matplotlib/animation.py", line 1703, in __init__
      TimedAnimation.__init__(self, fig, **kwargs)

      File "matplotlib/animation.py", line 1465, in __init__
      event_source = fig.canvas.new_timer()

      AttributeError: 'NoneType' object has no attribute 'new_timer'


      So I think I understood the error as animation needs a timer but I don't know how to fix it. Any help? Thank's in advance










      share|improve this question















      I'm trying to import a matplotlib 3D animation inside a Tkinter window but I got stuck on an error. My Tkinter window has many frames and I try to plot the animation on one of this frame. Finally, I create all elements inside a class init function. So here is a simplified version of my code:



      from tkinter import *
      import matplotlib
      matplotlib.use("TkAgg")
      from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
      from matplotlib.figure import Figure
      from mpl_toolkits.mplot3d import Axes3D
      import matplotlib.animation as anim
      import random

      class UI:
      def __init__(self):
      self.back = Frame(master=self.window, width=1200, height=800, bg='white')
      self.rightBack = Frame(master=self.back, width=300, height=800)
      self.rightBack.pack(side='right',padx=2,pady=2)
      self.fig = Figure()
      self.axes = Axes3D(self.fig)

      self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False) # Creating animation

      self.canvas = FigureCanvasTkAgg(self.fig, master=self.displayBack)
      self.canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=True)


      # Starting application
      self.window.mainloop()

      def updateDisplay(self,num):
      self.axes.clear()
      self.axes.plot([0,1,random.random()],[0,1,random.random()],[0,1,random.random()],'o',color='k',markersize='5')


      But when I run the following in python



      self.ani = anim.FuncAnimation(self.fig, self.updateDisplay, interval=100, blit=False)


      I get this error



      File "matplotlib/animation.py", line 1703, in __init__
      TimedAnimation.__init__(self, fig, **kwargs)

      File "matplotlib/animation.py", line 1465, in __init__
      event_source = fig.canvas.new_timer()

      AttributeError: 'NoneType' object has no attribute 'new_timer'


      So I think I understood the error as animation needs a timer but I don't know how to fix it. Any help? Thank's in advance







      python animation matplotlib tkinter 3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 13:21









      ImportanceOfBeingErnest

      125k10128204




      125k10128204










      asked Nov 20 '18 at 9:57









      Bucky

      102




      102
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The animation needs to reside in a figure that has a canvas. In your code you define the canvas only after the animation. I suppose it would work as expected if you reverse the order of the animation and the canvas.



          # First define the canvas
          self.canvas = FigureCanvasTkAgg(self.fig, ...)
          # only then define the animation
          self.ani = anim.FuncAnimation(self.fig, ...)





          share|improve this answer





















          • Yes I did this and it's incredibly working well! Thank you
            – Bucky
            Nov 21 '18 at 2:42











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390414%2fhow-to-import-a-matplotlib-3d-animation-in-a-tkinter-frame-in-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The animation needs to reside in a figure that has a canvas. In your code you define the canvas only after the animation. I suppose it would work as expected if you reverse the order of the animation and the canvas.



          # First define the canvas
          self.canvas = FigureCanvasTkAgg(self.fig, ...)
          # only then define the animation
          self.ani = anim.FuncAnimation(self.fig, ...)





          share|improve this answer





















          • Yes I did this and it's incredibly working well! Thank you
            – Bucky
            Nov 21 '18 at 2:42
















          0














          The animation needs to reside in a figure that has a canvas. In your code you define the canvas only after the animation. I suppose it would work as expected if you reverse the order of the animation and the canvas.



          # First define the canvas
          self.canvas = FigureCanvasTkAgg(self.fig, ...)
          # only then define the animation
          self.ani = anim.FuncAnimation(self.fig, ...)





          share|improve this answer





















          • Yes I did this and it's incredibly working well! Thank you
            – Bucky
            Nov 21 '18 at 2:42














          0












          0








          0






          The animation needs to reside in a figure that has a canvas. In your code you define the canvas only after the animation. I suppose it would work as expected if you reverse the order of the animation and the canvas.



          # First define the canvas
          self.canvas = FigureCanvasTkAgg(self.fig, ...)
          # only then define the animation
          self.ani = anim.FuncAnimation(self.fig, ...)





          share|improve this answer












          The animation needs to reside in a figure that has a canvas. In your code you define the canvas only after the animation. I suppose it would work as expected if you reverse the order of the animation and the canvas.



          # First define the canvas
          self.canvas = FigureCanvasTkAgg(self.fig, ...)
          # only then define the animation
          self.ani = anim.FuncAnimation(self.fig, ...)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 14:17









          ImportanceOfBeingErnest

          125k10128204




          125k10128204












          • Yes I did this and it's incredibly working well! Thank you
            – Bucky
            Nov 21 '18 at 2:42


















          • Yes I did this and it's incredibly working well! Thank you
            – Bucky
            Nov 21 '18 at 2:42
















          Yes I did this and it's incredibly working well! Thank you
          – Bucky
          Nov 21 '18 at 2:42




          Yes I did this and it's incredibly working well! Thank you
          – Bucky
          Nov 21 '18 at 2:42


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390414%2fhow-to-import-a-matplotlib-3d-animation-in-a-tkinter-frame-in-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out