Simple word cloud in Python. 💡 Wordcloud is a technique for…
By: Zolzaya Luvsandorj
Tags: Python, NLP, Visualisation, Word Cloud

2. Word cloud ☁️

Firstly, let’s prepare a function that plots our word cloud:

# Import packages

import matplotlib.pyplot as plt

%matplotlib inline # Define a function to plot word cloud

def plot_cloud(wordcloud):

# Set figure size

plt.figure(figsize=(40, 30))

# Display image

plt.imshow(wordcloud)

# No axis details

plt.axis("off");

Secondly, let’s create our first word cloud and plot it:

# Import package

from wordcloud import WordCloud, STOPWORDS # Generate word cloud

wordcloud = WordCloud(width = 3000, height = 2000, random_state=1, background_color='salmon', colormap='Pastel1', collocations=False, stopwords = STOPWORDS).generate(text) # Plot

plot_cloud(wordcloud)

Ta-da❕ We just built a word cloud! Here are some notes regarding the arguments for WordCloud function:

◼️ width/height: You can change the word cloud dimension to your preferred width and height with these.

◼️ random_state: If you don’t this set this to a number of your choice, you are likely to get a slightly different word cloud every time you run the same script on the same input data. By setting this parameter, you ensure reproducibility of the exact same word cloud. You could play around with random numbers until you find the one that results in the word cloud you like.

◼️ background_colour: ‘white’ and ‘black’ are common background colours. If you would like to explore more colours, this may come in handy. Please note that some colours may not work. Hope you will find something you fancy.

◼️ colormap: With this argument, you can set up the colour theme that the words are displayed in. There are many beautiful Matplotlib colormaps to choose from. Some of my favourites are ‘rainbow’, ‘seismic’, ‘Pastel1’ and Pastel2’.

◼️ collocations: Set this to False to ensure that the word cloud doesn’t appear as if it contains any duplicate words. Otherwise, you may see ‘web’, ‘scraping’ and ‘web scraping’ as a collocation in the word cloud, giving an impression that words have been duplicated.

◼️ stopwords: Stopwords are common words which provide little to no value to the meaning of the text. ‘We’, ‘are’ and ‘the’ are examples of stopwords. I have explained stopwords in more detail here (scroll to ‘STEP3. REMOVE STOPWORDS’ section). Here, we used STOPWORDS from the wordcloud package. To see the set of stopwords, use print(STOPWORDS) and to add custom stopwords to this set, use this template STOPWORDS.update(['word1', 'word2']) , replacing word1 and word2 with your custom stopwords before generating a word cloud.

There are other arguments that you can also customise. Check out the documentation for more information.

Let’s generate another word cloud with a different background_colour and colormap 🎨. You could play with different combinations until you find the one that you like. I find the following combination quite nice:

# Generate wordcloud

wordcloud = WordCloud(width = 3000, height = 2000, random_state=1, background_color='black', colormap='Set2', collocations=False, stopwords = STOPWORDS).generate(text) # Plot

plot_cloud(wordcloud)

Suppose we are happy with the word cloud and would like to save it as a .png file, we can do so using the code below: