我想用matplotlib.animation模块为python实时绘制一条数据行。我的代码概述是,我正在计算将其存储在列表(“my_average”)中的分数a,这是y-坐标。分数总是在-1和+1之间。此外,我还希望我的x坐标是我的列表的长度"(len(my_average))".Ex;列表接收到一个分数x-坐标将是列表的长度,所以1和y-坐标将是得分,list接收第二个分数,列表图(1,score1) (2,score2)等等。我无法显示图表,需要帮助处理我的代码的这一部分。此外,如果可能的话,我不想从csv文件中读取列表,而是直接从内存中读取,并且仍然能够查看历史上以前的数据点。
以下是代码:
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'])提前谢谢你。
发布于 2015-07-05 17:39:20
我没有一个twitter键来测试你的完整代码,但是这里有一个代码示例,您可以用它来修改一个情节。这种方法的一个缺点是,您需要终止该过程以停止动画。此示例将平均值与原始数据一起绘制出来,以进行说明。
import numpy
from pylab import *
import time
class StdOutListener():
def __init__(self):
self.start_time = time.time()
self.x = []
self.y = []
self.my_average = []
self.line_actual, = plot(self.x, self.y) # line stores a Line2D we can update
self.line_average, = plot(self.x, self.my_average) # line stores a Line2D we can update
def on_data(self, new_value):
time_delta = time.time() - self.start_time # on our x axis we store time since start
self.x.append(time_delta)
self.y.append(new_value)
self.my_average.append(numpy.mean(self.y))
self.line_actual.set_data(self.x, self.y)
self.line_average.set_data(self.x, self.my_average)
ylim([min(self.y), max(self.y)]) # update axes to fit the data
xlim([min(self.x), max(self.x)])
draw() # redraw the plot
ion() # ion() allows matplotlib to update animations.
out_listener = StdOutListener()
for i in range(1000):
out_listener.on_data(i + numpy.random.randint(-5,5))https://stackoverflow.com/questions/31128227
复制相似问题