Back to Templates

Create a Word Cloud

Create a Word Cloud to visualize the most frequent words in a text file. Word Clouds are also known as Tag Clouds and are a useful tool for initial visual exploration of text data in any NLP project. Word Clouds are typically used to depict keyword metadata on websites, or to visualize free form text. Important words are highlighted with a bigger font or stronger color.

%%capture 
!pip install wordcloud
# Load packages
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
Matplotlib is building the font cache; this may take a moment.
# Upload your data as a .txt file and load it as a data frame 
text = open('biden.txt', 
            mode='r', 
            encoding='utf-8') \
            .read().replace('\n','')
text[:1000]
"Chief Justice Roberts, Vice President Harris, Speaker Pelosi, Leader Schumer, Leader McConnell, Vice President Pence, and my distinguished guests, and my fellow Americans: This is America's day. This is democracy's day, a day of history and hope, of renewal and resolve. Through a crucible for the ages America has been tested anew, and America has risen to the challenge.Today we celebrate the triumph not of a candidate, but of a cause, the cause of democracy. The people—the will of the people has been heard, and the will of the people has been heeded. We've learned again that democracy is precious, democracy is fragile. And at this hour, my friends, democracy has prevailed.So now, on this hallowed ground where just a few days ago violence sought to shake the Capitol's very foundation, we come together as one Nation under God, indivisible, to carry out the peaceful transfer of power as we have for more than two centuries. As we look ahead in our uniquely American way—restless, bold, opti"
# change the value to black
def black_color_func(word, font_size, position,orientation,random_state=None, **kwargs):
    return("hsl(0,100%, 1%)")

wc = WordCloud(background_color="white",           # select background color
               width=3000,                         # set wight
               height=2000,                        # set height
               max_words=500).generate(text)       # set max amount of words

wc.recolor(color_func = black_color_func)          # set the word color to black
plt.figure(figsize=[15,10])                        # set the figsize
plt.imshow(wc, interpolation="bilinear")           # plot the wordcloud
plt.axis("off")                                    # remove plot axes
plt.savefig('wordcloud.png') 
Python

Create a Word Cloud

Visualize the most frequent words in a text file with a Word Cloud.

Use Template