Line plot is not appearing during animation

I am currently attempting to create an animation for a basic projectile motion, but I am encountering some problems in displaying the line that connects the markers. Although the markers are visible in the plot, the line does not appear. I have attempted to fix the issue by restarting the kernel and re-running the code, but it has not resolved the problem.
I have attached the code and the animation that I got.

%matplotlib ipympl

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

th = 45
angle = np.deg2rad(th)
v = 20
g = 10
tm = (2*v*sin(angle)/g) ## total time of flight
R = v**2*sin(2*angle)/g
Hm = (v*sin(angle))**2/(2*g)
fig = plt.figure(figsize= (10,6))
ax = fig.add_subplot()

def animate(t):
    x = v*cos(angle)*t
    y = v*sin(angle)*t - 0.5*g*t**2

    ax.plot(x,y, 'k-', lw = 1, marker = 'o', mfc = 'blue', mec = 'k')  
    ax.set_title(f'Horizontal Range = {x:.2f}m, time of flight = {t:0.2f}s')
    ax.set_ylim(0, Hm+0.5)
    return ax

fig.suptitle('Projectile Motion')
anim = FuncAnimation(fig, animate, frames=np.linspace(0, tm, 50),interval = 100, repeat = False)'projectile.gif', writer='pillow')                     


When the markers in the plot are removed, the plot disappears.

ax.plot(x,y, 'k-')  


Whereas If I try to plot a simple plot without any animation then the line is appearing.

plt.plot(np.arange(1,10), np.random.randint(10,20, 9), 'k-', lw = 1, marker = 'o', mfc = 'blue', mec = 'k')


Could you kindly provide me with suggestions on how to resolve this issue?
I’m running the code in Jupyter Notebook(version 7+)
matplotlib version - 3.8.4

Hello @pushpa, it looks like you are only plotting a single point at a time within your animation, so for each frame Matplotlib only knows about that one point and doesn’t know to join it onto the existing ones. It will likely be better to call ax.plot outside animate function and then update the line within animate. See this example: Animated line plot — Matplotlib 3.8.4 documentation

Yeah, I worked it out. Thanks.

1 Like