I am wanting to plot a line from my data in real-time using matplotlib.animation module for python. The overview of my code is that I am calculating a score a storing it in a list("my_average") and this is the y-coordinate. The score will always be between -1 & +1. further more I would like my x-coordinate to be the length of my list "(len(my_average))".Ex; list recieves one score x-coordinate would the length of the list so 1 and y-coordinate would be the score, list receives second score, list plots (1, score1) (2, score2), etc. I am not able to show the graph and need help with this portion of my code. Further more if possible I would not like to read the list from a csv file but rather directly from memory and still be able to view previous data points in history.
Here is the code:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
from textblob import TextBlob
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import numpy
# Variables that contains the user credentials to access Twitter API
access_token =
access_token_secret =
consumer_key =
consumer_secret =
# This is a basic listener that just prints received tweets to stdout.
my_list = [] #creates empty list
my_average = []
class StdOutListener(StreamListener):
def on_data(self, data):
json_load = json.loads(data)
texts = json_load['text'] # string
#print(texts)
wiki = TextBlob(texts)
r = wiki.sentiment.polarity
my_list.append(r)
#drop zero in list
if 0 in my_list: my_list.remove(0)
print (my_list)
#calculate average
average = numpy.mean(my_list)
b = my_average.append(average)
#drop "nan" from list
if 'nan' in my_average: my_average.remove('nan')
print "average", my_average
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, -10))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
def animate(i):
x = (len(my_average))
y = (my_average)
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,frames=100, interval=20, blit=True)
plt.show()
return True
def on_error(self, status):
print(status)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, StdOutListener())
# This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(track=['USD/CAD', 'Dollar', 'Loonie' ], languages=['en'])
Thank you in advance.