How to loop plot graph from matplotlib without having to close window?

627 views Asked by At

I am trying to continuously plot data points from a list (y-axis) against length of the list ("my_list"). I am confused if at all it is possible for matplotlib to continuously plot a graph without, me having to close the graph window each time physically? Also would it be possible to graph the variable "average" at the same time against variable "len(my_list)".

Here is my 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

# 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'])

Thank you in advance

0

There are 0 answers