Create a Tag Cloud of Tweets Using Python

No comments
Here I will show you a simple code (the full version is on GitHub: to create a tag cloud based on the content of tweets talking about a given query and save it as a png.

Before starting you will need to install some Python libraries:

First of all you will need to authenticate using the 4 secrets associated to your Twitter developer account (more info here:

 my_oauth = OAuth1(***,  

Then you will need to create the URL associated with your query:

 complete_url = '  

Here for example we are retrieving the most recent tweets containing the world "hello".

After that, you will just need to create your tag cloud, here we are using a loop to update the tagcloud every wait_time milliseconds.

 while True:  
   my_text = ''  
   r = requests.get(complete_url, auth=my_oauth)  
   tweets = r.json()  
   for tweet in tweets['statuses']:  
     text = tweet['text'].lower()  
     text = ''.join(ch for ch in text if ch not in punctuation) # exclude punctuation from tweets  
     important_words = text  
     my_text += important_words  
   words = my_text.split()  
   counts = Counter(words)  
   for word in stop_words:  
     del counts[word]  
   for key in counts.keys():  
     if len(key) < 3 or key.startswith('http'):  
       del counts[key]  
   final = counts.most_common(max_words)  
   max_count = max(final, key=operator.itemgetter(1))[1]  
   final = [(name, count / float(max_count))for name, count in final]  
   tags = make_tags(final, maxsize=max_word_size)  
   create_tag_image(tags, 'cloud_large.png', size=(width, height), layout=layout, fontname='Lobster', background = background_color)  
   print "new png created"  

If you want to download the complete version of the code and try it yourself you can find it here:

Feel free to comment if you encounter any problem.

No comments :

Post a Comment