Create Stunning Word Clouds with Python’s stylecloud in Two Lines
Learn how to install and use the Python stylecloud package to generate visually appealing word clouds with custom shapes, palettes, and masks, including examples using Trump’s speech and Tencent comments, plus tips on font paths, icon selection, and advanced parameters for fine‑tuned results.
This article introduces stylecloud, a Python package created by data scientist Max Woolf that extends the classic wordcloud library with additional features for producing unique and attractive word clouds.
stylecloud Overview
Unlike the basic wordcloud, stylecloud can render clouds in arbitrary icon shapes (via Font Awesome), supports advanced palettes (through palettable), provides gradient options, and offers a command‑line interface.
Icon‑shaped masks of any size (Font Awesome 5.11.2)
Advanced color palettes via palettable Direct gradient control
Input from plain text files or CSV files containing words and frequencies
Command‑line interface
Two‑Line Installation & Usage
pip3 install stylecloudAfter installation, a word cloud can be generated with just two lines of code:
from stylecloud import gen_stylecloud
gen_stylecloud(file_path='Trump.txt')The example uses the speech of Donald Trump ("Trump.txt") to produce a quick word cloud.
Custom Masks
Word‑cloud aesthetics can be dramatically improved by selecting a mask icon from Font Awesome. For example, to use a dog icon:
stylecloud.gen_stylecloud(
text=' '.join(text1),
collocations=False,
font_path=r'C:\Windows\Fonts\msyh.ttc',
icon_name='fas fa-dog',
size=400,
output_name='wordcloud.png'
)Brand icons such as QQ require the fab prefix (e.g., icon_name='fab fa-qq').
Palette Customisation
Colors are controlled via palettable. Changing the palette argument swaps the colour scheme:
stylecloud.gen_stylecloud(
text=' '.join(text1),
collocations=False,
palette='tableau.BlueRed_6',
font_path=r'C:\Windows\Fonts\msyh.ttc',
icon_name='fab fa-qq',
size=400,
output_name='Tencent-wordcloud.png'
)Other Useful Parameters
text: input text (best used with the function call)
file_path: path to a text or CSV file (best used via CLI)
gradient: direction of colour gradient (default: None)
size: width and height of the output image (default: 512)
icon_name: Font Awesome icon name for the mask (default: fas fa-flag)
palette: colour palette from palettable (default: cartocolors.qualitative.Bold_6)
background_color: background colour (default: white)
max_font_size: maximum font size in the cloud (default: 200)
max_words: maximum number of words (default: 2000)
stopwords: whether to filter common stopwords (default: True)
output_name: filename for the generated image (default: stylecloud.png)
font_path: path to a .ttf font file (default: built‑in Staatliches font)
random_state: seed for reproducibility
Final Example
The following code reproduces the Tencent comment word cloud shown in the article:
import stylecloud
from IPython.display import Image
stylecloud.gen_stylecloud(
text=' '.join(text1),
collocations=False,
font_path=r'C:\Windows\Fonts\msyh.ttc',
icon_name='fab fa-qq',
size=400,
output_name='Tencent-wordcloud.png'
)
Image(filename='Tencent-wordcloud.png')All data files and source code are available for download at:
https://alltodata.cowtransfer.com/s/53bee58e15c54b
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.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
