Table of contents
1.
Introduction
2.
Getting Started: Libraries and Installation
3.
Basic Word Cloud Creation
3.1.
Step-by-step Process:
3.2.
Customizing Your Word Cloud
4.
Advanced Features
4.1.
Removing Stopwords:
4.2.
Including Word Frequencies:
5.
Practical Example: Word Cloud from a Web Page
6.
Frequently Asked Questions
6.1.
Can I generate word clouds for non-English texts?
6.2.
How can I save the generated word cloud to an image file?
6.3.
Can I change the color palette of the word cloud?
7.
Conclusion
Last Updated: Aug 28, 2025
Easy

Word Cloud in Python

Author Sinki Kumari
0 upvote

Introduction

A word cloud is a visual representation of text data, typically used to highlight frequently appearing words in a body of text. The importance of each word is represented by its size and color. In this article, we'll dive into how to generate word clouds in Python.

Word Cloud in Python

Getting Started: Libraries and Installation

To create a word cloud in Python, the primary library used is wordcloud. You can install it via pip:

pip install wordcloud
You can also try this code with Online Python Compiler
Run Code

Basic Word Cloud Creation

Step-by-step Process:

Importing Necessary Libraries: Begin by importing the WordCloud class and other necessary libraries.

from wordcloud import WordCloud
import matplotlib.pyplot as plt
You can also try this code with Online Python Compiler
Run Code

 

Preparing Your Text Data: This can be any text – a file, a string, etc.

text = "Python is great. Python is dynamic. Python is an interpreted language."

Generating the Word Cloud: Use the WordCloud class.

wordcloud = WordCloud().generate(text)
You can also try this code with Online Python Compiler
Run Code

 

Displaying the Word Cloud: Utilize matplotlib.

plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
You can also try this code with Online Python Compiler
Run Code

Customizing Your Word Cloud

Changing Appearance:

You can modify the appearance, such as the font size, background color, and more:

wordcloud = WordCloud(
    background_color='white',
    max_words=100,
    max_font_size=50,
    scale=5,
    random_state=1
).generate(text)
You can also try this code with Online Python Compiler
Run Code

 

Including a Mask:

To generate a word cloud that fits into a custom shape (e.g., a star):
 

from wordcloud import STOPWORDS
import numpy as np
from PIL import Image
mask = np.array(Image.open("star.png"))
wordcloud = WordCloud(background_color="white", mask=mask, stopwords=STOPWORDS).generate(text)
You can also try this code with Online Python Compiler
Run Code

Advanced Features

Removing Stopwords:

STOPWORDS is a set of words like "and", "the", etc., that might be repetitive and not insightful in your cloud.

from wordcloud import STOPWORDS
stopwords = set(STOPWORDS)
wordcloud = WordCloud(stopwords=stopwords).generate(text)
You can also try this code with Online Python Compiler
Run Code

Including Word Frequencies:

Instead of directly providing raw text, you can also provide word frequencies:
 

word_frequencies = {
    'Python': 5,
    'dynamic': 3,
    'interpreted': 2,
    'language': 2,
    'great': 1
}
wordcloud = WordCloud().generate_from_frequencies(word_frequencies)
You can also try this code with Online Python Compiler
Run Code

Practical Example: Word Cloud from a Web Page

Let's generate a word cloud from a Wikipedia page:

import requests
from bs4 import BeautifulSoup
from wordcloud import WordCloud, STOPWORDS  # Import the WordCloud class
import matplotlib.pyplot as plt  # Import the matplotlib.pyplot module


# Fetching text data from a Wikipedia page
response = requests.get('https://en.wikipedia.org/wiki/Python_(programming_language)')
soup = BeautifulSoup(response.text, 'html.parser')
text = soup.get_text()


# Removing stopwords and generating the word cloud
stopwords = set(STOPWORDS)
wordcloud = WordCloud(stopwords=stopwords).generate(text)


# Displaying the cloud
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

Can I generate word clouds for non-English texts?

Yes! The WordCloud library supports various languages, but you may need to set stopwords for the respective language.

How can I save the generated word cloud to an image file?

Use the to_file() method: wordcloud.to_file("my_wordcloud.png").

Can I change the color palette of the word cloud?

Yes, you can utilize the colormap parameter in the WordCloud class and set it to any of the Matplotlib colormaps.

Conclusion

In this article, we looked at the process of creating word clouds in Python, showing a simple yet effective method for visualizing the most common words in a dataset. With the help of Python’s powerful libraries, we learned to transform the raw text into good graphical representations. These word clouds make data analysis more engaging and help quickly identify key themes and patterns, enhancing both the accessibility and insightfulness of textual data.

Recommended Readings:

Reverse a string in python

Making Word Cloud in Tableau

What is a Dictionary in Python

Live masterclass