create-word-cloud-python

Introduction

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

 

Implementation

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

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 examples that show how we can generate word clouds in different shapes like rectangles, circles, and arbitrary shapes.

 

Create Word Cloud in Python Examples

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-rectangle

 

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-circle

 

Example 3:

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

black-bottle

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

 

Conclusion

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

Happy Coding 🙂

Check out our posts:

Leave a Reply

Your email address will not be published.