0

Click Here for the image

trying to plot an animated line chart in python. Why is this code returning to a blank white plot ? a guidance would be appreciated. And also if there is a better way to draw an animated line chart in Python, please suggest.Thank you.

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

x_data=[]
y_data =[]
fig,ax = plt.subplots()
ax.set_xlim(0,100)
ax.set_ylim(0,12)
line, = ax.plot(0,0)

def update(i):
    x_data.append(i*10)
    y_data.append(i)

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line,

animation = FuncAnimation(fig,func = update, frames = np.arange(0,10,0.01), interval =200)

plt.show()

1 Answers1

0

The code works for me, but is very slow because you have added 1000 frames at 200ms intervals, so the full animation takes 200 seconds to complete.

You need the imports at the top (and the appropriate libraries installed)

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

x_data = []
y_data = []
fig, ax = plt.subplots()
ax.set_xlim(0, 100)
ax.set_ylim(0, 12)
line, = ax.plot(0, 0)

def update(i):
    x_data.append(i*10)
    y_data.append(i)

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line,


animation = FuncAnimation(fig,func = update, frames = np.arange(0, 10, 0.01), interval = 2)

plt.show()

I have set the interval to 2ms in the above code to show a faster animation.

Graeme Stuart
  • 5,837
  • 2
  • 26
  • 46
  • hii @GraemeStuart thanks for the reply. Yeah, the required libraries are installed and imported properly. Just tried with this exact code with interval = 2, but still getting a blank white plot. I have tried on VSCode and also on Jupyter NB from Anaconda navigator. I am wondering if it can be a problem in Jupyter nb or not – Writabrata Jun 08 '22 at 07:03
  • Added an image if it helps. And also If there's a more efficient or easy way to plot an animated line chart please suggest. – Writabrata Jun 08 '22 at 07:38