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 CodeCustomizing 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 CodeAdvanced 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 CodeIncluding 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 CodePractical 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 CodeFrequently 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