Fundamentals 7 min read

Create Stunning Word Clouds in Python: Step‑by‑Step Guide with WordCloud Library

Learn how to install the Python wordcloud module, generate custom word clouds from text, customize fonts, colors, backgrounds, and stopwords, and integrate with Pillow and matplotlib, all illustrated with code snippets and images, to visualize text data effectively.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Create Stunning Word Clouds in Python: Step‑by‑Step Guide with WordCloud Library

Introduction

Hello, I'm Huang Wei. A word cloud visualizes the most frequent keywords in a text, highlighting them for quick insight.

1. Installing the wordcloud module

pip install wordcloud

2. Importing and initializing the WordCloud object

Import the library and create a WordCloud instance, optionally specifying a custom font.

import wordcloud
wc = wordcloud.WordCloud(font_path=r'C:\Windows\Fonts\华康少女文字简W5.ttc', width=200, height=100)  # initialization

3. Adding text to the word cloud

Generate the cloud from a string.

wc.generate('任性的90后boy')

4. Saving and displaying the word cloud

Save the image to a file.

wg.to_file('gf.jpg')

5. Changing the background color

wordcloud.WordCloud(font_path=r'C:\Windows\Fonts\华康少女文字简W5.ttc', width=200, height=100, background_color="gray")

6. Customizing colors with ImageColorGenerator

Use a background image to derive colors.

from wordcloud import ImageColorGenerator, WordCloud as wc
from PIL import Image
import numpy as n
ff = open('OSI.txt','r',encoding='utf-8').read()
bg = n.array(Image.open('g.png'))
w = wc(font_path=r'C:\Windows\Fonts\华康少女文字简W5.ttc', mode='RGBA', mask=bg, repeat=True, background_color='#FFFFFF')
wg = w.generate(ff)
ig = ImageColorGenerator(bg)
wg.recolor(color_func=ig)
wg.to_file('gf.png')

7. Using stopwords

Exclude specific words from the cloud.

sd = STOPWORDS.add('Control')
wc = WordCloud(font_path=r'C:\Windows\Fonts\华康少女文字简W5.ttc', mode='RGBA', mask=bg, width=500, height=300, repeat=True, stopwords=sd, background_color='#FFFFFF')

8. Extending with Pillow and matplotlib

Open and display the generated image.

im = wordcloud.wordcloud.Image.open('q.jpg')
im.show()

Conclusion

Word clouds provide a simple way to visualize the proportion of terms in a text, serving as an intuitive data‑analysis tool.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData visualizationtext miningpillowwordcloud
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.