简体   繁体   English

Matplotlib python动画没有显示行

[英]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 用于绘图的窗口显示但没有出现,我得到这个ValueError:x和y必须具有相同的第一个维度

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()

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. 我必须为点显示的唯一更改是为plot命令添加标记样式。

This is because when you call plot you are plotting a new line . 这是因为当您调用plot您正在绘制一条新线 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, eg 将标记样式更改为显示点的样式可以修复此问题,例如

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

causes points to be plotted in red circles. 导致点以红色圆圈绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM