Introduction

Word Cloud is a visual representation of text data that helps to get a high level understanding about the important keywords from data in terms of their occurrence. They are most commonly used to highlight popular or trending terms based on frequency of use and prominence.

Implementation

For implementation of word cloud in python, we will be using following python packages:

  • wordcloud
  • matplotlib
  • numpy
  • pillow

To install those packages simply run these commands in your working directory.

pip install wordcloud

pip install matplotlib

pip install numpy

pip install pillow

Here, in this post we will be working on three example that shows how we can generate word clouds in different shapes like rectangle, circle and arbitrary shapes. 

Example 1 :

word_rect.py

from wordcloud import WordCloud
import matplotlib.pyplot as plt

# Read the text from document ‘sample_text.txt’
text = open('sample_text.txt', encoding="utf8").read()

# Generate a word cloud image of document
wordcloud = WordCloud(width=780, height=405,
 background_color="#ffde59").generate(text)

# Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

Output :

word cloud in rectangle shape

 

Example 2 :

word_circle.py

import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud

# Read the text from document ‘sample_text.txt’
text = open('sample_text.txt', encoding="utf8").read()

# makes the circle using numpy
x, y = np.ogrid[:300, :300]
mask = (x - 150) ** 2 + (y - 150) ** 2 > 130 ** 2
mask = 255 * mask.astype(int)

# Generate a word cloud of document
wordcloud = WordCloud(background_color="white",
 mask=mask, contour_width=0.1, contour_color="black")
wordcloud.generate(text)

#Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

Output :

word cloud in circle shape

 

Example 3 :

In this example, by using mask we can create word cloud in arbitrary shapes. Here we use this black bottle for masking sample text.

black bottle masking

word_arbit.py

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS

# Read the text from document ‘sample_text.txt’
text = open('sample_text.txt', encoding="utf8").read()

# read the mask image from bottle
bottle_mask = np.array(Image.open("bottle.png"))

stopwords = set(STOPWORDS)

wordcloud = WordCloud(background_color="white", mask=bottle_mask,
               stopwords=stopwords, contour_width=3, contour_color='steelblue')

# generate word cloud
wordcloud.generate(text)

# Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

Output :

word cloud bottle shape

Conclusion 

Hence, we were successful to generate word cloud in different shapes.

Happy Coding :-)