Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to smoothly plot the moving dot

I want to plot a moving dot from left to right. Here’s my code:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

Acc_11 = [0,1,2,3,4,5,6,7,8]
Acc_12 = [4,4,4,4,4,4,4,4,4]
fig = plt.figure()
axes = fig.add_subplot(111, autoscale_on=False)
axes.set_xlim(min(Acc_11), max(Acc_11))

axes.set_ylim(0, 8)


point, = axes.plot([Acc_11[0]],[Acc_12[0]], 'go')

def ani(coords):
   point.set_data([coords[0]],[coords[1]])
   return point,

def frames():
   for acc_11_pos, acc_12_pos in zip(Acc_11, Acc_12):
       yield acc_11_pos, acc_12_pos

ani = FuncAnimation(fig, ani, frames=frames, interval=300)

plt.show()

However, the dot stops at each point then continue, but I want the dot moving smoothly in this speed without changing the interval. Can anyone please help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"Smooth" would always require "more frames" in my opinion. So I do not see a way to make the movement smoother, i.e. increase the number of frames, without increasing the frames per second, i.e. changing the interval.

Here’s a version with frames increased tenfold and interval reduced tenfold:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

Acc_11 = np.linspace(0,8,90)  # increased frames
Acc_12 = np.ones(len(Acc_11))*4
fig = plt.figure()
axes = fig.add_subplot(111, autoscale_on=False)
axes.set_xlim(min(Acc_11), max(Acc_11))

axes.set_ylim(0, 8)


point, = axes.plot([Acc_11[0]],[Acc_12[0]], 'go')

def ani(coords):
   point.set_data([coords[0]],[coords[1]])
   return point,

def frames():
   for acc_11_pos, acc_12_pos in zip(Acc_11, Acc_12):
       yield acc_11_pos, acc_12_pos

ani = FuncAnimation(fig, ani, frames=frames, interval=30)  # decreased interval

plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading