Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Matplotlib';动画功能?_Python_Matplotlib_Matplotlib Animation - Fatal编程技术网

Python 如何使用Matplotlib';动画功能?

Python 如何使用Matplotlib';动画功能?,python,matplotlib,matplotlib-animation,Python,Matplotlib,Matplotlib Animation,有人能看到我的代码到底出了什么问题吗?我正在尝试生成一个我的结果的动画图,以查看其随时间的变化,结果只是我的数据的完整图,而不是更新的动画 sampleText.txt文件只是一个大小为(5000,1)的numpy数组,数组的前5个条目是[-0.01955058],[0.00658392[,[-0.00658371],-0.0061325],-0.0219136] import matplotlib.animation as animation import time fig = plt.f

有人能看到我的代码到底出了什么问题吗?我正在尝试生成一个我的结果的动画图,以查看其随时间的变化,结果只是我的数据的完整图,而不是更新的动画

sampleText.txt
文件只是一个大小为
(5000,1)
的numpy数组,数组的前5个条目是
[-0.01955058],[0.00658392[,[-0.00658371],-0.0061325],-0.0219136]

import matplotlib.animation as animation
import time

fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot()

def animate(i):
    
    x = np.linspace(0, len(dataArray), len(dataArray))
    y = []

    for eachLine in x:
        y.append(eachLine * 10)
            
    ax1.clear()
    ax1.plot(x, y, color = 'red', alpha = 0.5)

ani = animation.FuncAnimation(fig, func = animate)

plt.show()


动画的基础是一种机制,其中动画函数设置初始图形设置要更改的值。在这种情况下,线宽为3,颜色为红色,x和y为空列表。xy数据通过动画函数中的i链接到帧数

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

fig = plt.figure() 

# marking the x-axis and y-axis 
axis = plt.axes(xlim =(0, 500), ylim =(0, 1000)) 

x = np.linspace(0, 500, 500) 
y = x*2
# initializing a line variable 
line, = axis.plot([], [], lw=2, color='r') 

def animate(i): 
    line.set_data(x[:i], y[:i]) 
    return line, 

anim = FuncAnimation(fig, animate, frames=500, blit=True, repeat=False) 
plt.show()

那有什么问题吗?你有一半的问题,一半的问题here@MadPhysicist请参阅我对问题的更新。我简化了示例并更清楚地说明了问题。我得到的是完整数据的绘图,而不是更新的动画,这正是我希望保持响应的原因。你能提供一个数据吗示例。例如调用
np.arange
np.random
,而不仅仅是口头描述?我不能100%确定您的意思,但我的示例是我的“(5000,1)”numpy数组的前5个条目看起来像“[[-0.01955058][0.00658392][0.00658371][-0.0061325][-0.0219136]”“你不需要精确的数组。只要提供一些类似的代码。请参阅参考资料。请参阅基础知识。如果我的答案对你有帮助,请考虑接受它作为正确的答案。我会检查我的工作,然后让你知道。谢谢你的回应。