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

时间:2017-07-26 21:38:59

标签: python valueerror

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

我收到错误:

ValueError: cannot copy sequence with size 78 to array axis with dimension 1

其中大小对应于std数组中的项目数。 (所以这当然,每次我点击Run都会增加。)

我想知道为什么我收到这个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()

1 个答案:

答案 0 :(得分:1)

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

std = pd.rolling_std(yar, 100)

但是,在运行pd.rolling_std()时会出现弃用警告。所以,该行应该是:

std = yar.rolling(window=100,center=False).std()

此外,关于如何生成xaryar,还可以进行一些简化。 xar只是一个范围,yar包含data允许您编写的所有元素:

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()已经有所简化。