Note
Go to the end to download the full example code.
Minimal Example
Generating a square wordcloud from the US constitution using default arguments.
import os
from os import path
from wordcloud import WordCloud
# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# Read the whole text.
text = open(path.join(d, 'werewolf.txt')).read()
# Generate a word cloud image
wordcloud = WordCloud(width=1600,height=900).generate(text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
stop_words = {"wouldn't", 'between', "why's", 'know', 'over', "you'll", "we'd",
"shouldn't", 'these', 'until', 'himself', 'no', "aren't", 'too', "doesn't",
"here's", 'monsterdondoublefeature', 'or', 'theirs', "he'll", 'very', "you're",
"hasn't", "when's", 'above', 'does', 'off', "where's", 'under', "hadn't",
'film', "didn't", 'it', 'any', 'he', 'http', 'such', 'got', 'wolfen1981', 'be',
'this', 'to', "we're", "who's", 'howling11985', 'www', 'a', 'not', "he'd",
'ours', 'you', 'his', 'would', 'then', "there's", 'out', 'so', 'my', "i'm",
'was', 'after', 'whom', 'we', 'your', 'all', 'about', 'ever', 'had', 'how',
'hers', 'other', 'up', "i'll", "i've", 'during', 'therefore', 'while', "we've",
"she's", 'can', 'their', "don't", 'since', 'did', 'once', "she'd", 'they',
'than', 'like', 'good', "you've", "they'd", 'are', 'her', "you'd", 'ourselves',
'movie', 'themselves', 'really', 'cannot', 'she', 'more', 'however', 'do',
'going', 'when', 'with', 'our', "we'll", 'com', 'have', 'the', 'yeah', "let's",
'those', 'them', "wasn't", 'which', 'why', 'yourselves', 'been', 'who', 'being',
'most', 'get', "mustn't", "what's", 'also', 'me', 'watch', 'its', 'again',
"haven't", "she'll", 'yours', 'that', 'as', 'before', 'hence', 'of',
'monstermiru', "they're", 'few', 'were', 'now', "isn't", 'one', 'both', 'well',
"weren't", 'could', 'if', "how's", 'there', 'am', 'because', 'see', 'further',
'wulfen1981', 'is', 'ought', 'herself', "they've", 'i', 'own', 'into', 'what',
'same', 'guy', 'otherwise', 'yourself', "couldn't", 'monsterdon', "it's", "i'd",
'wrongwolfen', 'nor', 'from', 'by', 'should', 'an', 'some', 'myself', 'where',
'down', 'shall', 'else', 'at', 'for', 'here', 'movies', 'against', 'through',
"they'll", 'in', 'on', "that's", 'has', "he's", 'doing', "won't", 'but', 'r',
'him', 'films', 'itself', 'and', 'having', 'each', 'only', "can't", 'k', 'just',
"shan't", 'below'}
# lower max_font_size
wordcloud = WordCloud(max_font_size=40,
stopwords=stop_words,
).generate(text)
plt.figure(figsize=(13, 9))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.savefig('werewolf.png', format="png")
# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()
Total running time of the script: (0 minutes 1.358 seconds)

