Skip to content Skip to sidebar Skip to footer

Valueerror: Cannot Copy Sequence To Array Axis In Python For Matplotlib Animation

I am reading in a constantly updated data.txt file and am calculating a rolling standard deviation on the incoming data stream. I am storing that in std. I have a window size of

Solution 1:

The reason for your error is that you stdbecomes a list (of one element) of a data frame. pd.rolling_std() is already giving you a data frame, and then you append it to your list. If you just directly assign it, it will work better:

std = pd.rolling_std(yar, 100)

However, when running pd.rolling_std() there is a deprecation warning given. So, the line should rather be:

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

Also, there are a few more simplifications that can be made as of how you generate xar and yar. xar is just a range and yar contains all the elements of data allowing you to write:

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

where animate() has been somewhat simplified.

Post a Comment for "Valueerror: Cannot Copy Sequence To Array Axis In Python For Matplotlib Animation"