ValueError:无法在Python中将序列复制到MatplotLib动画的数组轴

ValueError:无法在Python中将序列复制到MatplotLib动画的数组轴,python,valueerror,Python,Valueerror,我正在读取一个不断更新的data.txt文件,并计算传入数据流的滚动标准偏差。我将其存储在std中。我有一个窗口大小为100的滚动标准差 我收到一个错误: ValueError:无法将大小为78的序列复制到维度为1的阵列轴上 其中,大小对应于std数组中的项数。(当然,每次我跑步时,这个数字都会增加) 我想知道为什么我会收到这个ValueError,我正在寻找任何修复它的建议!当我刚刚对ax1.plot(xar,yar)进行分级时,动画效果很好。。但是,一旦我尝试用图表来绘制ax1.plot(x

我正在读取一个不断更新的
data.txt
文件,并计算传入数据流的滚动标准偏差。我将其存储在
std
中。我有一个窗口大小为100的滚动标准差

我收到一个错误:

ValueError:无法将大小为78的序列复制到维度为1的阵列轴上

其中,大小对应于
std
数组中的项数。(当然,每次我跑步时,这个数字都会增加)

我想知道为什么我会收到这个ValueError,我正在寻找任何修复它的建议!当我刚刚对ax1.plot(xar,yar)进行分级时,动画效果很好。。但是,一旦我尝试用图表来绘制ax1.plot(xar,std),问题就出现了

data.txt
中的数据如下所示:

[0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625]
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    data = pd.read_csv("C:\\Users\\Desktop\\data.txt", sep="\[|\]\[|\]",engine = 'python', header = None)
    data = data.iloc[0]
    data = data.astype(str).apply(lambda x: x.split(',')[-1]).astype(float)
    data.pop(0)
    xar = []
    yar = []
    std = []
    for j in range(len(data)):
        xar.append(j)
    for k in range(len(data)):
        yar.append(data.iloc[k])
    yar = pd.DataFrame(yar)
    std.append(pd.rolling_std(yar, 100))
    ax1.clear()
    ax1.plot(xar,std)
ani = animation.FuncAnimation(fig, animate, interval=.01)
plt.show()
我目前的代码如下:

[0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0, 0.0078125, 0.015625][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0244140625, 0.0322265625, 0.9609375][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0263671875, 0.0341796875, 1.0341796875][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.0244140625, 0.0341796875, 1.0048828125][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.033203125, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.0263671875, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.0107421875][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625][0.025390625, 0.0341796875, 1.009765625]
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    data = pd.read_csv("C:\\Users\\Desktop\\data.txt", sep="\[|\]\[|\]",engine = 'python', header = None)
    data = data.iloc[0]
    data = data.astype(str).apply(lambda x: x.split(',')[-1]).astype(float)
    data.pop(0)
    xar = []
    yar = []
    std = []
    for j in range(len(data)):
        xar.append(j)
    for k in range(len(data)):
        yar.append(data.iloc[k])
    yar = pd.DataFrame(yar)
    std.append(pd.rolling_std(yar, 100))
    ax1.clear()
    ax1.plot(xar,std)
ani = animation.FuncAnimation(fig, animate, interval=.01)
plt.show()

出现错误的原因是
std
成为数据帧的列表(一个元素)
pd.rolling_std()
已经为您提供了一个数据帧,然后您将其附加到列表中。如果您直接分配它,它会工作得更好:

std = pd.rolling_std(yar, 100)
但是,当运行
pd.rolling_std()
时,会发出弃用警告。因此,这条线应该是:

std = yar.rolling(window=100,center=False).std()
此外,对于如何生成
xar
yar
,还可以进行一些简化
xar
只是一个范围,
yar
包含
数据的所有元素,允许您编写:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def animate(i):
    data = pd.read_csv("C:\\Users\\Desktop\\data.txt", sep="\[|\]\[|\]",engine = 'python', header = None)
    data = data.iloc[0]
    data = data.astype(str).apply(lambda x: x.split(',')[-1]).astype(float)
    data.pop(0)
    xar = range(len(data))
    yar = pd.DataFrame(data)
    std = yar.rolling(window=100,center=False).std()
    ax1.clear()
    ax1.plot(xar,std)

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ani = animation.FuncAnimation(fig, animate, interval=.01)
plt.show()

其中的
animate()
已经稍微简化了。

太棒了!这项工作做得很出色。谢谢你的支持!像你这样的人让这个社区非常棒。在动画的一开始,有一个很大的尖峰。我怎样才能删除动画中的初始尖峰?我不知道您的确切用例,所以我不知道。但是,当使用滚动平均值时,在有足够多的数据点获得第一个平均值(在上面的代码中为100)之前,在开始时有一堆nan(不是数字)值。因此,当您碰到100个元素时,可能会有一个凹凸,从nan值变为实际值。你们看到的可能是这个吗?这意味着对它并没有什么可做的,因为它是数据本身的属性。可以在初始平均值中使用较少的值,从1增加到100,但我不知道这是否更好。这需要更多的逻辑。也许你可以在这里提出一个后续问题来支持我: