DEV Community

Cover image for word clouds with python ☁️🐍

Posted on

word clouds with python ☁️🐍

Creating Word Clouds with WordCloud module in Python

Word clouds are a visually appealing way to represent the frequency of words in a text. They help in quickly identifying the most prominent terms, providing insights into the main themes and topics. In this article, we'll explore how to generate a word cloud using the wordcloud library in Python. We'll also use matplotlib for displaying the word cloud.


To follow along, ensure you have the following libraries installed:

pip install wordcloud 
pip install matplotlib
Enter fullscreen mode Exit fullscreen mode

</> Example Code

Below is a Python script that generates a word cloud from a given text about Steve Jobs. The script leverages the wordcloud library to create the word cloud and matplotlib to display it.

import matplotlib.pyplot as plt
from wordcloud import WordCloud

text = '''
Steven Paul Jobs was an American businessman, inventor, and investor best known for co-founding the technology giant Apple Inc. Jobs was also the founder of NeXT and chairman and majority shareholder of Pixar. 
He was a pioneer of the personal computer revolution of the 1970s and 1980s, along with his early business partner and fellow Apple co-founder Steve Wozniak.
Jobs was born in San Francisco in 1955 and adopted shortly afterwards. He attended Reed College in 1972 before withdrawing that same year. In 1974, he traveled through India, seeking enlightenment before later studying Zen Buddhism. 
He and Wozniak co-founded Apple in 1976 to further develop and sell Wozniak's Apple I personal computer. Together, the duo gained fame and wealth a year later with production and sale of the Apple II, one of the first highly successful mass-produced microcomputers. 
Jobs saw the commercial potential of the Xerox Alto in 1979, which was mouse-driven and had a graphical user interface (GUI). This led to the development of the unsuccessful Apple Lisa in 1983, followed by the breakthrough Macintosh in 1984, the first mass-produced computer with a GUI. 
The Macintosh launched the desktop publishing industry in 1985 with the addition of the Apple LaserWriter, the first laser printer to feature vector graphics and PostScript.

wc = WordCloud().generate(text)
plt.figure(figsize=(10, 5))
Enter fullscreen mode Exit fullscreen mode




  • Import Libraries: We import matplotlib.pyplot for visualization and WordCloud from the wordcloud library for generating the word cloud.
  • Text Data: The variable text contains a multiline string about Steve Jobs, which serves as the input for our word cloud.
  • Generate Word Cloud: The WordCloud().generate(text) method creates the word cloud from the input text.
  • Display Word Cloud: We use matplotlib to display the generated word cloud. plt.imshow(wc) displays the image, and plt.axis('off') removes the axes for a cleaner look.

WordCloud Parameters

The WordCloud class provides several parameters to customize the appearance and behavior of the word cloud. Below is a table summarizing the key parameters:

Parameter Description
width Width of the canvas on which to draw the word cloud, default is 400.
height Height of the canvas on which to draw the word cloud, default is 200.
max_words Maximum number of words to include in the word cloud, default is 200.
stopwords Words to be excluded from the word cloud, default is STOPWORDS.
background_color Background color for the word cloud image, default is "black".
colormap Colormap to use for the word cloud, can be any matplotlib colormap, default is "viridis".
max_font_size Maximum font size for the largest word, default is None.
min_font_size Minimum font size for the smallest word, default is 4.
random_state Random state for reproducibility of the layout, default is None.
contour_color Color of the contour line around the words, default is None.
contour_width Width of the contour line around the words, default is 0 (no contour).

→ By adjusting these parameters, you can fine-tune the appearance of your word cloud to better suit your needs.


Word clouds are a powerful tool for visualizing text data, and the wordcloud library in Python makes it easy to generate them. By following the steps outlined above, you can create your own word clouds and customize them using various parameters. Whether you're analyzing large text corpora or simply looking to create a visually appealing summary of text, word clouds are a great choice.

→ Feel free to experiment with different texts and WordCloud parameters to see how they affect the final output. Happy coding!

About Me


Top comments (0)