
As a Data Scientist or even Data Enthusiast, there is a time when you would work with a Text Analysis Project. During this analysis, one thing we always have done is present the most frequent word in the form of word clouds.
For you who did not know what a word cloud is, you could check the image above. It is an example of a word cloud. The more frequent the word is the bigger their size in the word cloud. This word cloud is useful for presenting your insight to attract any unaware audience.
The problem is, most of the word cloud presentation is bland and boring—it just a box of words with no power to catch the audience’s mind. Just look at the word cloud below.

While you look at the word cloud above, you realize that it is a word cloud from the Grimm’s Brother Cinderella story. You only realize that because the frequent word is showing the Cinderella word.
Well, for a catchy presentation, we could present it in some other shape. For example, in the form of the slipper.

So, how we reshape our word cloud into a nicer form?
stylecloud
We could create a cool word cloud by using the python package out there called stylecloud.
This package offers us to create a quick, stylish word cloud in a few lines. Let’s start by installing the package. For you who have not owned the base word cloud package yet, we could download it as well.
pip install wordcloud
pip install stylecloud
When you have installed the packages, we can jump to create our word cloud. To define which shape of the word cloud you want, you need to find the icon you want from the fontawesome website.
Here, you need to choose the icon and find the class parameter above the icon. It should show something like this.

After that, you need to prepare your intended text to show up in your word cloud. In my case, I would try to present the Grimm’s Red Riding Hood word cloud.
Now, let’s try to run the command.
import stylecloud
#We would use gen_stylecloud to shape our word cloud. I put my text #into a variable called "red".
#For parameter, icon_name is the class name from the font awesome that you choose and output_name is the name of the file
stylecloud.gen_stylecloud(red, icon_name= "fab fa-wolf-pack-battalion", output_name = 'red.png')
If you use the same icon as I did, it will show the word cloud just like this.

The output is an image file in your local folder. You can find it in the same folder as your jupyter notebook or any IDE you use.
So, how about if you want to create a word cloud but with your own shape? In this case, we need to work around it a little bit.
Word Cloud with our own shape
When we create a shape for our word cloud, we actually create a mask as a guide to place our words.
To masking the word cloud, we need to create the mask first from our intended image you want. In my case before, it is Cinderella’s heel, which just looks like this.

To make sure that the word cloud has the proper shape you want, you need to have a full colored (any color except white) for the shape and all-white (#FF or #FFFFFF) for the background any place you don’t want the word placed.
With the image ready, now we can try to create the mask first. The mask needs to convert into an array of the number first. We would use the Numpy and Image from the PIL package to do that.
import numpy as np
from PIL import Image
#My file name is called 'cinderella-heel.png'
mask = np.array(Image.open('cinderella-heel.png'))
When we have the mask, we can create the shaped word cloud now. You can follow my command line to create the word cloud. I would use the Cinderella story I previously mentioned and input it in the variable called ‘cinder.’
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
#Here we specify the width, height, background color, stopwords to use and the mask. The words
word_cloud = WordCloud(width = 500, height = 500, background_color='white', stopwords=STOPWORDS, mask=mask).generate(cinder)
plt.figure(figsize=(10,8))
plt.imshow(word_cloud)
plt.axis('off')
plt.tight_layout(pad=0)
plt.show()

And that is it. Now you are capable of creating a catchy presentable word cloud to show in your presentation.
Conclusion
Word cloud is often used in text analysis for finding an exciting insight. The problem is, it was too bland and boring.
For that reason, we could shape the word cloud into a nicer form for a better aesthetic.
We can use the stylecloud to create a quick nice word cloud or use the masking technique to have your own shape.
I hope it helps!
If you enjoy my content and want to get more in-depth knowledge regarding data or just daily life as a Data Scientist, please consider subscribing to my newsletter here.
If you are not subscribed as a Medium Member, please consider subscribing through my referral.