I would like my output from my StdOutListener to be plotted live. The problem is, that the twitter stream itself is a loop that can not be terminated multiple times in a row to update the graph, so I have to stop the matplotlib function somehow. i tried it with
I tried it with
plt.show(block=False) but this results in multiple matplotlib windows opening. I tried stopping and starting the matplotlib loob, but this doesn't seem to have any effect.
Currently, I am executing my live_Graph class after every incoming tweet, because it seems to be the only option to connect the two loops.
class StdOutListener(StreamListener): def on_data(self,data): try: all_data = json.loads(data) text=all_data["text"] temp_text=TextBlob(text) analysis=temp_text.sentiment.polarity output = open("twitter-out.txt","a") output.write(str(analysis)) output.write("\n") output.close() print("sucsess") live_graph=LiveGraph() plt=live_graph.analyze_data() return True except BaseException as e: print('Failed: ', str(e)) class LiveGraph(): def animate(i): graph_data=open("twitter-out.txt", "r").read() lines=graph_data.split("\n") xar= yar= x=0 y=0 for l in lines: print(l) x+=1 try: y=float(l) print(y) except BaseException as e: pass xar.append(x) yar.append(y) #xs = graph_data.index.tolist() #ys = graph_data["Sentiment"].tolist() ax1.clear() ax1.plot(xar, yar) ani = animation.FuncAnimation(fig, animate, interval=3000) plt.show()
As a result, I'd like to have the graph updating itself, in the same file that it gets the tweets from. My problem is, that I am still quite new to python and forking did not seem to be an option due to its complex nature.