How to Easily Make Beautiful Wordclouds in Python | by Frank Andrade | Towards Data Science

A wordcloud lets us easily identify the keywords in a text where the size of the words represents their frequency. With this, we’ll get a good idea of what a text is about before even reading it. Although there are many free tools to make wordclouds online, we can use our Python skills to customize wordclouds even more.
In this article, we’re going to use the stylecloud library to make beautiful wordclouds with a few lines of code, the wordcloud library to add our own image to use it as a mask of our wordcloud and stop_words to customize the stopwords lists offered by default.
The text we’re going to use to make a wordcloud is part of Steve Job’s speech at Standford. You can find the .txt file on my Github or use any text to make your own wordcloud.
To easily make beautiful wordclouds, the first thing we have to do is to install stylecloud. You can install it via pip pip3 install stylecloud.
Once you installed stylecloud, you have to import it.
import stylecloud
Now to create a basic wordcloud we need to use the .gen_stylecloud() method and add the path of the .txt file and the icon we’re going to use to give form to this wordcloud. On this website, you can find a list of icons that you can use with stylecloud. Just search the icon you want and copy the HTML code of the icon (in this example, I chose an apple as an icon)
stylecloud.gen_stylecloud(file_path='SJ-Speech.txt',                          icon_name= "fas fa-apple-alt")
The result is the wordcloud below (the file is created in the same path where your Python script is located)
But we can do even more! We can customize the wordcloud further to obtain a better-looking wordcloud.
We can control the background color, the color of the words, the name of the output file, and also omit collocations of two words in the wordcloud. To do so, we’ll use the code below.
stylecloud.gen_stylecloud(file_path='SJ-Speech.txt',                          icon_name='fas fa-apple-alt',                          colors='white',                          background_color='black',                          output_name='apple.png',                          collocations=False)
We can even write HTML code of colors instead of using standard colors like “white.” On this website, you can pick any color you want and get its HTML code that you can include in the colors parameter of stylecloud.
Stylecloud has by default a list of stopwords in the English language, but in case you want to remove or add new stopwords you have to include a new list in the custom_stopwords parameter.
To customize a list of stopwords, we have to use the stop_words library. To do so, first, install it (pip install stop_words) Then we use get_stop_words to get a list of stop words in a specific language.
from stop_words import get_stop_wordsstop_words = get_stop_words('english')
Once you defined the list, you can add or remove elements with the .append and .remove methods respectively.
stop_words.append('my_new_stop_word')stop_words.remove('my_new_stop_word')
Now, we only have to add the new stop_words list. I'm also going to use a palette instead of colors to give the wordcloud a different look.
stylecloud.gen_stylecloud(file_path='SJ-Speech.txt',                          icon_name='fas fa-apple-alt',                         palette='cartocolors.qualitative.Pastel_3',                          background_color='black',                          output_name='apple.png',                          collocations=False,                          custom_stopwords=stop_words)
On this website, you can find a list of color palettes for Python that you can use with the stylecloud library (as you can see in the code above, the names separated by dots represent the content of the palettes module)
There are hundreds of free icons available to use with stylecloud but sometimes we might want to use our own image to give form to the wordcloud we want to create. If that’s the case, we have to use the wordcloud library to make a wordcloud, PIL to upload an image, matplotlib to plot the wordcloud and numpy to use the image uploaded as a mask of our wordcloud.
First, we install all of the 4 libraries mentioned before. Once we have all of them, we import them and start creating a wordcloud with the desired mask.
from wordcloud import WordCloud, ImageColorGenerator from PIL import Imageimport matplotlib.pyplot as pltimport numpy as np# create a mask based on the image we wish to includemy_mask = np.array(Image.open('batman-logo.png'))# create a wordcloud wc = WordCloud(background_color='white',               mask=my_mask,               collocations=False,               width=600,               height=300,               contour_width=3,               contour_color='black',               stopwords=stop_words)
As you can see, the code used is similar to stylecloud with a few variations. We use mask to specify the image we want to use for the wordcloud (in this case, I’m using the batman logo). Also, we included the contour width and color to make the shape of the image more visible.
Note: Make sure that the image you will use as a mask has a well-defined shape with clear boundaries otherwise the wordcloud will not look good.
Now we have to read the text file and generate the wordcloud with the following code.
with open('SJ-Speech.txt', 'r') as txt_file:    texto = txt_file.read()wc.generate(texto)
In case we want to use the original image’s color, we write the following code (if not the wordcloud will have random colors just like the first apple image shown in this article)
image_colors = ImageColorGenerator(my_mask)wc.recolor(color_func=image_colors)
Finally to plot the wordcloud we use matplotlib.
plt.figure(figsize=(20, 10))plt.imshow(wc, interpolation='bilinear')plt.axis('off')wc.to_file('wordcloud.png')plt.show()
I also remove the axis to only have the wordcloud in the plot and save the wordcloud withwc.to_file()
That’s it! Now you’re ready to make your own wordcloud with Python! I created some wordclouds using Simpsons characters in the article below.
towardsdatascience.com
Here is a list of books I read to learn Python.
betterprogramming.pub
Join my email list with 3k+ people to get my Python for Data Science Cheat Sheet I use in all my tutorials (Free PDF)
Thanks for reading this article! In the articles below, you can find other guides I made to create beautiful visualization in Python.