How to Install and Fix WordCloud in Python for Chinese Text Visualization

This tutorial walks you through installing the WordCloud library, resolving the Microsoft Visual C++ compiler error, fixing Chinese font issues, and creating both basic and image‑masked word clouds with sample Python code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Install and Fix WordCloud in Python for Chinese Text Visualization

1. Installing WordCloud

Before using WordCloud, install the required packages with 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 CSDN download link is provided.

2. Simple Word Cloud Example

The following example demonstrates how to generate a basic word cloud from Chinese text. WordCloud visualizes high‑frequency keywords as a colorful cloud.

# example code (illustrative)
from wordcloud import WordCloud
import jieba

text = "your Chinese text here"
words = " ".join(jieba.cut(text))
wc = WordCloud(font_path='msyh.ttf', width=800, height=400).generate(words)
wc.to_file('wordcloud.png')

3. Fixing Chinese Font Issues

After installation, the default font (DroidSansMono.ttf) does not support Chinese characters. Edit wordcloud.py and change the FONT_PATH to point to a Chinese font such as msyh.ttf (Microsoft YaHei).

Place the msyh.ttf file in the same directory as wordcloud.py so the library can load it.

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

4. Word Cloud with Image Background

You can create a word cloud shaped by a background image (e.g., sss3.png). The core code loads the image as a mask and generates the cloud.

# illustrative code
from wordcloud import WordCloud
import numpy as np
from PIL import Image

mask = np.array(Image.open('sss3.png'))
wc = WordCloud(mask=mask, font_path='msyh.ttf').generate(words)
wc.to_file('cloud_with_image.png')

Running the above steps produces word clouds that can visualize topics from CSDN blog posts, chat records, and other Chinese text sources.

For more advanced usage, refer to the original blog and the author's WeChat live broadcast.

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 miningwordcloudChinese encodingtext 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.