How to Install, Fix, and Use WordCloud in Python for Chinese Text Visualization
This tutorial walks you through installing the WordCloud library, resolving common Windows compilation errors, handling Chinese font encoding issues, and creating both basic and image‑masked word clouds in Python, complete with code examples and visual results.
1. Installing WordCloud
Before generating word clouds, install the required packages with pip:
pip install WordCloud pip install jieba
WordCloud creates the visual cloud, 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) from a reliable source such as the CSDN link provided.
2. Simple WordCloud Example
A basic script (shown in the original image) imports WordCloud and generates a cloud from a list of words. The resulting image displays the most frequent Chinese keywords in a colorful cloud.
3. Solving Chinese Encoding Errors
After installation, edit the wordcloud.py file located in the library directory. Change the FONT_PATH from the default DroidSansMono.ttf to msyh.ttf (Microsoft YaHei) to support Chinese characters.
Place the msyh.ttf file in the same directory as the script. Then you can generate a cloud without encoding issues using:
wordcloud = WordCloud(font_path='MSYH.TTF').fit_words(word)
4. WordCloud with Image Mask
To create a word cloud shaped by a background image (e.g., sss3.png), use the mask parameter in the WordCloud constructor. The core code (illustrated in the original image) loads the image, creates a mask array, and passes it to WordCloud.
The final output displays a cloud that conforms to the silhouette of the provided picture, as shown in the example results.
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.
