How to Install and Fix WordCloud in Python for Chinese Text Visualization
This guide walks you through installing the Python WordCloud library, resolving common compilation errors, handling Chinese font encoding issues, and creating basic and image‑masked word clouds, complete with code snippets and troubleshooting tips for smooth visualization of Chinese text data.
This article explains how to set up and use the WordCloud library in Python for Chinese text visualization, including installation, error handling, font configuration, and example code.
1. Install WordCloud
Before using WordCloud you need to install the package and its dependencies.
pip install WordCloud pip install jiebaWordCloud generates word clouds, while jieba provides Chinese word segmentation.
If you encounter the error “error: Microsoft Visual C++ 9.0 is required…”, install the Microsoft Visual C++ Compiler for Python 2.7 (VCForPython27). A download link is provided.
2. Simple word‑cloud code
Example code (adapted from the referenced blog) creates a word cloud from Chinese text.
from wordcloud import WordCloud
import jieba
text = "your Chinese text here"
words = jieba.cut(text)
word = " ".join(words)
wc = WordCloud(font_path='msyh.ttf', width=800, height=400).generate(word)
wc.to_file('wordcloud.png')The resulting image displays the most frequent keywords.
3. Fixing Chinese encoding errors
Open the installed wordcloud.py file and change the FONT_PATH from DroidSansMono.ttf to msyh.ttf (Microsoft YaHei). Place the msyh.ttf file in the same directory.
4. Word cloud with image mask
Using an image mask (e.g., sss3.png) you can shape the word cloud.
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(word)
wc.to_file('masked_wordcloud.png')Sample results are shown below.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
