I would like to display time series data of stock prices and averages in a Python 3 project.
The data is currently stored in a CSV file which is updated periodically and my interpreter is Anaconda.
I've tried using
Matplotlib.animation.FuncAnimation()
However the Figure window pops up without any axes and then fails to respond and crashes.
Here is my Charting Class in the Project:
class Chart:
import matplotlib.pyplot
import matplotlib.animation as animation
plt = matplotlib.pyplot
@classmethod
def __init__(cls):
fig = cls.plt.figure(figsize=(5, 5))
global ax1
ax1 = fig.add_subplot(1, 1, 1)
ani = cls.animation.FuncAnimation(fig, cls.animate, interval=1000, blit=True)
cls.plt.show()
@classmethod
def animate(cls, i):
xs = []
ys = []
ax1.clear()
data = pd.read_csv('chart_values.csv')
date = data['timestamp']
last = data['last']
short_ma = data['short_ma']
long_ma = data['long_ma']
xs.append(date)
ys.append(last)
ax1.clear()
ax1.plot(xs, ys)
ax1.set_title('Trade data')
Note *
Another issue I encountered was that I needed to set an environment PATH variable that pointed to /library/plugins in my anaconda dir which solves the problem of getting this error message:
this application failed to start because it could not find or load the qt platform plugin "windows" in "".
However it needs to be deleted as soon as id like to use any other program which requires PyQt.
* EDIT *
I've changed the code as per the responses but have yet to get the plot to load and display data continuously.
class Chart:
import matplotlib.pyplot
import matplotlib.animation as animation
plt = matplotlib.pyplot
def __init__(self):
fig = self.plt.figure(figsize=(5, 5))
self.ax1 = fig.add_subplot(1, 1, 1)
self.line, = self.ax1.plot([], [])
ani = self.animation.FuncAnimation(fig, self.update, interval=1000, blit=True)
self.plt.show()
def update(self, i):
# I don't think its essential to pass the loc as a param just yet.
data = pd.read_csv('chart_values.csv', header=0)
self.line.set_data(data['timestamp'], data['last'])
self.ax1.set_title('Trade data')
I get the following errors after I KeyboardInterrupt the program :
ValueError: could not convert string to float: '2020-05-21T17:04:13.645Z'
As well as:
RuntimeError: The animation function must return a sequence of Artist objects.