omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Animated graph with matplotlib.animation

    Pythonista
    4
    6
    3261
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • epalzeo
      epalzeo last edited by ccc

      Hello

      i've code in python 3 to make animated graph with datas coming in realtime

      for this, i use

      ani = FuncAnimation(fig, update, frames=np.linspace(0,40, 4096),
                          init_func=init, blit=True)
      plt.show()
      

      with update function where i update the graph objects

      it works well on my desktop

      In pythonista it shows the first frame then exit like the animation loop is not implemented. It does the init function only

      Do you have any clue on this issue ?

      Thanks in advance :)

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        You might try with the backend_pythonista, however many features are not implemented (like blit).

        Plt.show shows a plot to the console, but it is a static image.

        For animating specific types of data, you will be better off going with a custom view, since vector ops are a lot faster than generating a whole image each frame.

        1 Reply Last reply Reply Quote 1
        • Kephy
          Kephy last edited by ccc

          Hi:
          Is not possible to animate graphs yet? I took some examples and they’re not working. I’m sharing one of them:

          import numpy as np
          import matplotlib.pyplot as plt
          import matplotlib.animation as animation
          
          def data_gen():
          	t = data_gen.t
          	cnt = 0
          	while cnt < 1000:
          		cnt+=1
          		t += 0.05
          		yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
          data_gen.t = 0
          
          fig, ax = plt.subplots()
          line, = ax.plot([], [], lw=2)
          ax.set_ylim(-1.1, 1.1)
          ax.set_xlim(0, 5)
          ax.grid()
          xdata, ydata = [], []
          def run(data):
          	# update the data
          	t,y = data
          	xdata.append(t)
          	ydata.append(y)
          	xmin, xmax = ax.get_xlim()
          
          	if t >= xmax:
          		ax.set_xlim(xmin, 2*xmax)
          		ax.figure.canvas.draw()
          	line.set_data(xdata, ydata)
          
          	return line,
          
          ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
          	repeat=False)
          plt.show()
          
          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @Kephy last edited by cvp

            @Kephy I don't know anything about matplotlib but try this little script, perhaps it could help you to start.

            from io import BytesIO
            import numpy as np
            import matplotlib.pyplot as plt
            import ui
            
            v = ui.ImageView()
            v.frame = (0,0,400,400)
            v.present('sheet')
            a = np.array([])
            
            fig, ax = plt.subplots()
            
            for i in range(20):
            	# update the data
            	a = np.append(a,np.random.randn(10))
            	plt.plot(a)
            	b = BytesIO()
            	plt.savefig(b)
            	plt.close(fig) # free memory
            	v.image = ui.Image.from_data(b.getvalue())
            
            1 Reply Last reply Reply Quote 0
            • Kephy
              Kephy last edited by

              Thanks. So grateful. ☺️

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @Kephy last edited by

                @Kephy see
                https://GitHub.com/jsbain/pythonista_matplotlib_backports

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors