Fundamentals 13 min read

Create Stunning Word Clouds with Python’s stylecloud, wordcloud, and pyecharts

This guide walks through generating word clouds from JD product reviews using Python, covering preprocessing, tokenization, frequency analysis, and visual creation with the stylecloud, wordcloud, and pyecharts libraries, including mask image selection, palette customization, and full code snippets for each method.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Create Stunning Word Clouds with Python’s stylecloud, wordcloud, and pyecharts

01 Word Cloud Overview

Word cloud is a visual representation of high‑frequency keywords, using text, color and shapes to create an impactful visual that conveys valuable information.

It highlights frequent words in a “keyword cloud”, allowing readers to grasp the main idea of a text at a glance.

This article demonstrates preprocessing JD product review data, tokenizing, counting word frequencies, and displaying the results with word clouds.

02 Using the stylecloud Library

1. Introduction to stylecloud

stylecloud is a Python package created by data‑scientist Max Woolf, extending wordcloud with additional features such as arbitrary icon shapes (via Font Awesome), advanced palettes (via palettable), gradient support, file input, and a CLI.

Installation

pip install stylecloud -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

Icon shapes from Font Awesome 5.11.2

Advanced palettes via palettable

Direct gradient support

Read text or CSV files

Command‑line interface

2. Mask Images

Mask images define the shape of the cloud; stylecloud can use Font Awesome icons directly, avoiding the need to create custom masks.

Example Font Awesome link: https://fontawesome.com/license/free

3. Color Palettes

stylecloud uses the palettable library for high‑quality palettes; see https://jiffyclub.github.io/palettable/.

4. Generating a Cloud

Key parameters of gen_stylecloud() include text, file_path, icon_name, palette, output_name, gradient, size, font_path, random_state, background_color, max_font_size, max_words, stopwords, custom_stopwords, etc.

# -*- coding: UTF-8 -*-
"""
@Author  :叶庭云
@CSDN   :https://yetingyun.blog.csdn.net/
"""
from stylecloud import gen_stylecloud
import jieba, re, random

# Read data
with open('datas.txt', encoding='utf-8') as f:
    data = f.read()

# Preprocess: keep only Chinese characters
new_data = "/".join(re.findall('[\u4e00-\u9fa5]+', data, re.S))

# Tokenize
seg_list_exact = jieba.cut(new_data, cut_all=True)

result_list = []
with open('stop_words.txt', encoding='utf-8') as f:
    stop_words = set(line.strip() for line in f)

for word in seg_list_exact:
    if word not in stop_words and len(word) > 1:
        result_list.append(word)

# Choose a palette, e.g. cartocolors.qualitative.Bold_5
gen_stylecloud(
    text=' '.join(result_list),
    size=600,
    font_path=r'C:\Windows\Fonts\msyh.ttc',
    output_name='词云.png',
    icon_name='fas fa-grin-beam',
    palette=cartocolors.qualitative.Bold_5
)

Resulting word cloud image:

03 Using the wordcloud Library

wordcloud is a popular third‑party library that creates a WordCloud object from text and supports size, shape, and color configuration.

pip install wordcloud -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

WordCloud() represents a cloud for a given text.

Frequency determines word size.

Shape, size, and color can be customized.

# -*- coding: UTF-8 -*-
"""
@Author  :叶庭云
@CSDN   :https://yetingyun.blog.csdn.net/
"""
import jieba, collections, re
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# Load 958 comments
with open('data.txt') as f:
    data = f.read()

# Keep only Chinese characters
new_data = " ".join(re.findall('[\u4e00-\u9fa5]+', data, re.S))

# Tokenize
seg_list_exact = jieba.cut(new_data, cut_all=True)

result_list = []
with open('stop_words.txt', encoding='utf-8') as f:
    stop_words = set(line.strip() for line in f)

for word in seg_list_exact:
    if word not in stop_words and len(word) > 1:
        result_list.append(word)

# Count frequencies
word_counts = collections.Counter(result_list)

my_cloud = WordCloud(
    background_color='white',
    width=900, height=600,
    max_words=100,
    font_path='simhei.ttf',
    max_font_size=99,
    min_font_size=16,
    random_state=50
).generate_from_frequencies(word_counts)

plt.imshow(my_cloud, interpolation='bilinear')
plt.axis('off')
plt.show()

Generated word cloud image:

04 Word Cloud with pyecharts

pyecharts is a Python wrapper for ECharts that supports interactive charts; its WordCloud component allows chainable configuration.

# -*- coding: UTF-8 -*-
"""
@Author  :叶庭云
@CSDN   :https://yetingyun.blog.csdn.net/
"""
import jieba, collections, re
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
from pyecharts import options as opts
from pyecharts.globals import ThemeType, CurrentConfig

CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/'

# Load data
with open('data.txt') as f:
    data = f.read()

new_data = " ".join(re.findall('[\u4e00-\u9fa5]+', data, re.S))

seg_list_exact = jieba.cut(new_data, cut_all=True)

result_list = []
with open('stop_words.txt', encoding='utf-8') as f:
    stop_words = set(line.strip() for line in f)

for word in seg_list_exact:
    if word not in stop_words and len(word) > 1:
        result_list.append(word)

word_counts = collections.Counter(result_list)
word_counts_top100 = word_counts.most_common(100)

word1 = WordCloud(init_opts=opts.InitOpts(width='1350px', height='750px', theme=ThemeType.MACARONS))
word1.add(
    '词频',
    data_pair=word_counts_top100,
    word_size_range=[15, 108],
    textstyle_opts=opts.TextStyleOpts(font_family='cursive'),
    shape=SymbolType.DIAMOND
).set_global_opts(
    title_opts=opts.TitleOpts('商品评论词云图'),
    toolbox_opts=opts.ToolboxOpts(is_show=True, orient='vertical'),
    tooltip_opts=opts.TooltipOpts(is_show=True, background_color='red', border_color='yellow')
)
word1.render('商品评论词云图.html')

Interactive word cloud rendered in a web page:

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.

Pythontext miningPyechartsword cloudstylecloud
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.