Fundamentals 5 min read

How to Install, Fix, and Use Python WordCloud – A Complete Guide

This tutorial walks you through installing the WordCloud and jieba packages, resolving common Windows compilation errors, handling Chinese font encoding issues, and creating both basic and image‑masked word clouds with practical code examples and screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Install, Fix, and Use Python WordCloud – A Complete Guide

1. Installing WordCloud

Before using WordCloud, install the required Python packages via pip:

pip install WordCloud pip install jieba

WordCloud generates word clouds, while jieba provides Chinese word segmentation.

If you encounter the error " error: Microsoft Visual C++ 9.0 is required ", download and install VCForPython27 (Microsoft Visual C++ Compiler for Python 2.7). A direct download link is provided in the original article.

Installation screenshots
Installation screenshots

2. Simple Word Cloud Code

A basic example (adapted from the referenced blog) creates a word cloud from a list of words:

from wordcloud import WordCloud import jieba text = "你的中文文本这里" words = jieba.lcut(text) word_freq = {w: words.count(w) for w in set(words)} wc = WordCloud(font_path='msyh.ttf', width=800, height=400, background_color='white') wc.generate_from_frequencies(word_freq) wc.to_file('wordcloud.png')

The resulting image displays the most frequent keywords in a colorful cloud.

Word cloud result
Word cloud result

3. Chinese Encoding Error and Fix

If the generated image shows garbled characters, edit the wordcloud.py file in the library directory. Locate the FONT_PATH variable and replace the default DroidSansMono.ttf with msyh.ttf (Microsoft YaHei).

Editing FONT_PATH
Editing FONT_PATH

Place the msyh.ttf file in the same directory as wordcloud.py so the library can locate the Chinese font.

Font file placement
Font file placement

After fixing the font, the word cloud renders Chinese characters correctly, as shown in the example analyzing multiple CSDN blog posts.

Correct Chinese word cloud
Correct Chinese word cloud

Alternatively, you can set the font directly when creating the WordCloud object:

wordcloud = WordCloud(font_path='MSYH.TTF').fit_words(word)

4. Word Cloud with Image Background

To shape the word cloud using a background image (e.g., sss3.png), use the following core code:

from wordcloud import WordCloud, ImageColorGenerator import numpy as np from PIL import Image mask = np.array(Image.open('sss3.png')) wc = WordCloud(font_path='msyh.ttf', background_color='white', mask=mask) wc.generate_from_frequencies(word_freq) wc.recolor(color_func=ImageColorGenerator(mask)) wc.to_file('cloud_masked.png')

The resulting image blends the word cloud with the shape of the provided picture, as demonstrated by the example showing chat records.

Masked word cloud 1
Masked word cloud 1
Masked word cloud 2
Masked word cloud 2

These steps provide a complete workflow for installing, troubleshooting, and customizing WordCloud in Python, especially for Chinese text analysis.

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.

encodingInstallationjiebawordcloudtext visualization
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.