I am quite new to python 3 and I am currently learning a bit about animation.FuncAnimation in matplotlib. I wrote a small program which plots the cpu usage over time. However, as more and more data is plotted, the graph compresses to the left. I want the x axis values to keep shifting as the data is updated. I think this question was raised here : Updating the x-axis values using matplotlib animation . However, I was not able to follow it that well. I want to achieve something like this
:: http://www.roboticslab.ca/matplotlib-animation/
It would be great if someone can help me with this. Thanks !
Here is my code:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import psutil
%matplotlib widget
fig = plt.figure()
ax1 = fig.add_subplot(111)
cpu = []
def animate(i):
cpu.append(psutil.cpu_percent())
ax1.clear()
ax1.plot(cpu)
ani = animation.FuncAnimation(fig, animate, interval = 1000)
plt.show()
Using Jupyter Lab 1.0.7
macOS 10.15