Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib python animation not showing line

The windows for plotting shows up but nothing appears and i get this ValueError: x and y must have same first dimension

import psutil
import matplotlib.pyplot as plt
import matplotlib.animation as animation

a = [i for i in range(1000)]
ram_avaliable = []
fig, ax = plt.subplots()

def update(n):
    ram = psutil.virtual_memory()
    ram_avaliable.append(float(ram[1])/1073741824)
    print (a[n],ram_avaliable[n])
    ax.plot(a[n],ram_avaliable[n])

ani = animation.FuncAnimation(fig,update,interval=100)
plt.show()
like image 417
user146335 Avatar asked Nov 25 '25 08:11

user146335


1 Answers

Your code as posted runs without errors for me. The only change I had to make for points to show up was add a marker style to the plot command.

This is because when you call plot you are plotting a new line. The reason it wasn't showing up before is because the default line style is to connect points with lines - since there is only one point per plotted line, there's nothing to connect to. Changing the marker style to one that shows points fixes this, e.g.

ax.plot(a[n],ram_avaliable[n], 'ro')

causes points to be plotted in red circles.

like image 179
user812786 Avatar answered Nov 27 '25 20:11

user812786



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!