Creating Stunning Wordclouds in Python with Stylecloud
Written on
Chapter 1: Introduction to Wordclouds
Wordclouds provide a visual representation of text, highlighting significant words based on their frequency. This allows us to quickly grasp the essence of a text without delving deeply into it. While numerous online tools exist for generating wordclouds, leveraging Python can offer far greater customization options.
In this guide, we'll utilize the stylecloud library to create visually appealing wordclouds with minimal code. Additionally, we will use the wordcloud library to incorporate custom images as masks and adjust the default stopwords through the stop_words library.
Section 1.1: Crafting Beautiful Wordclouds with Stylecloud
For our wordcloud, we'll use an excerpt from Steve Jobs' commencement address at Stanford. You can download the .txt file from my GitHub repository or select any text of your choice.
To kick off, the first step is to install the stylecloud library using the following command:
pip3 install stylecloud
After installation, you need to import the library into your Python environment:
import stylecloud
Next, to generate a basic wordcloud, we will employ the .gen_stylecloud() method, specifying the path to the .txt file and selecting an icon for the shape of the wordcloud. For example, we might choose an apple icon. The code snippet below illustrates this:
stylecloud.gen_stylecloud(file_path='SJ-Speech.txt',
icon_name="fas fa-apple-alt")
The resulting wordcloud will be created in the same directory as your script.
However, we can enhance our wordcloud further by customizing its appearance. By controlling aspects like the background color and text colors, we can create a more appealing final product. Here's how you can do that:
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)
You can even utilize HTML color codes instead of standard color names to achieve a specific look.
Section 1.2: Customizing Stopwords
The default stopwords in stylecloud cover common English words. If you wish to modify this list, you can do so by importing the stop_words library and creating a custom list. First, install the library:
pip install stop_words
Then, you can generate a list of stopwords for English:
from stop_words import get_stop_words
stop_words = get_stop_words('english')
You can add or remove words from this list using the .append() and .remove() methods:
stop_words.append('my_new_stop_word')
stop_words.remove('my_new_stop_word')
Finally, integrate your customized stopwords list into the stylecloud function:
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)
Section 1.3: Using Your Own Image
If you'd like to use a personal image as the shape of your wordcloud, you can do so with the wordcloud library, along with PIL for image processing, matplotlib for plotting, and numpy for image manipulation.
Start by installing these libraries if you haven't already. Then, import them as shown below:
from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
To use an image as a mask, load it with numpy:
my_mask = np.array(Image.open('batman-logo.png'))
Next, create the wordcloud with the specified mask:
wc = WordCloud(background_color='white',
mask=my_mask,
collocations=False,
width=600,
height=300,
contour_width=3,
contour_color='black',
stopwords=stop_words)
Ensure your chosen mask image has a clear shape for the best results.
Now, read the text file and generate the wordcloud:
with open('SJ-Speech.txt', 'r') as txt_file:
texto = txt_file.read()
wc.generate(texto)
To maintain the original colors of your image, apply the following:
image_colors = ImageColorGenerator(my_mask)
wc.recolor(color_func=image_colors)
Finally, visualize and save your wordcloud with matplotlib:
plt.figure(figsize=(20, 10))
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
wc.to_file('wordcloud.png')
plt.show()
That's it! You're now equipped to create your own stunning wordclouds using Python. For further inspiration, check out my other projects featuring wordclouds with characters from The Simpsons.
Here’s a list of books that have helped me learn Python.
Join my email newsletter with over 3,000 subscribers to receive my free Python for Data Science Cheat Sheet, which I reference in all my tutorials.
Thank you for reading! Explore the related articles below for more tips on creating captivating visualizations in Python.
Chapter 2: Video Tutorials
The following videos will provide additional insights and techniques for crafting wordclouds in Python.
Learn how to create a stunning wordcloud using Python's stylecloud library in this comprehensive tutorial.
Discover how to create wordclouds in various shapes with Python, enhancing your data visualization skills.