我试图连续绘制列表中的数据点(y轴)与列表的长度(" my_list")。我很困惑,如果matplotlib可以连续绘制图形,我不得不每次都关闭图形窗口吗?也可以绘制变量" average"同时反对变量" len(my_list)"。
这是我的代码:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
from textblob import TextBlob
import matplotlib.pyplot as plt
# 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
class StdOutListener(StreamListener):
def on_data(self, data):
json_load = json.loads(data)
texts = json_load['text'] # string
#coded = texts.encode('utf-8') #byte
#print(texts)
wiki = TextBlob(texts)
r = wiki.sentiment.polarity
my_list.append(r)
print ("Polarity score",r)
average = 0
sum = 0
for r in my_list:
sum = r + r
average = sum / len(my_list)
print ("this is my average",average)
plt.plot((len(my_list)), (average), 'ro')
plt.axis([0,10,0,10])
plt.show(block=False)
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=['dollar', 'euro', 'loonie', 'Greece Debt' ], languages=['en'])
提前谢谢