stylecloud:简洁易用的词云库
source link: http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA%3D%3D&%3Bmid=2650073991&%3Bidx=1&%3Bsn=5e0d2af9bba4b795396425b5dc559e41
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
预祝全国考生:鲤鱼跃龙门,门门考高分~
stylecloud基于wordcloud库,使用方法更简单一些。该库的特点有
-
支持词云图图标形状设置
-
可直接读取csv文件(csv有两列,word和freq)
-
可调色
-
...
安装
pip install stylecloud
数据
csv为词频统计结果,两个字段,分别为 word和freq 。
今天准备了两个csv
-
data/高考.csv
-
data/股市.csv
import pandas as pd df1 = pd.read_csv('data/高考.csv') df1.head()word freq 0 高考 2198 1 未来 1549 2 决定 1443 3 一个 806 4 可以 644
df2 = pd.read_csv('data/股市.csv') df2.head()word freq 0 牛市 1993 1 图片 649 2 市场 530 3 A股 527 4 股市 520
快速上手
import stylecloud stopwords = open('data/stopwords.txt', encoding='utf-8').read().split('\n') stylecloud.gen_stylecloud(file_path='data/高考.csv', font_path='data/SourceHanSansCN-Regular.otf', output_name='output/高考1.png', size=500, custom_stopwords=stopwords)
import stylecloud stopwords = open('data/stopwords.txt', encoding='utf-8').read().split('\n') stylecloud.gen_stylecloud(file_path='data/股市.csv', font_path='data/SourceHanSansCN-Regular.otf', output_name='output/股市1.png', size=500, custom_stopwords=stopwords)
stylecloud参数介绍
-
file_path:CSV的文件路径,需要注意的是csv文件格式为两列(word, freq)
-
size:词云图尺寸[默认值:512]
-
custom_stopwords:自定义停用词列表
-
output_name:输出的词云图路径
-
font_path:字体路径
-
icon_name:图标名(fas fa-iconname)只需要改iconname,[默认值: fas fa-flag]
-
palette:调色板(通过 palettable 实现)[默认值:cartocolors.qualitative.Bold_6]
-
background_color:词云图背景色[默认值:white]
-
max_font_size:词云图最大字号[默认值:200]
-
random_state:控制单词和颜色的随机状态,如不设置,每次运行的效果会发生变化
图形设置
icon_name参数的模板【fas fa-iconname】只需要改动iconname即可。
iconname并不是随便起的,必须能在https://fontawesome.com/license/free 搜到才可以。
比如以 高考 为例,我会在搜索框搜索跟 教育 相关的关键词
-
university
-
education 等
截图中找到最能体现文本主题的图标 user-graduate
所以icon_name = 'fas fa-user-graduate'
import stylecloud stopwords = open('data/stopwords.txt', encoding='utf-8').read().split('\n') stylecloud.gen_stylecloud(file_path='data/高考.csv', font_path='data/SourceHanSansCN-Regular.otf', output_name='output/高考2.png', icon_name='fas fa-user-graduate', size=500, custom_stopwords=stopwords)
import stylecloud stopwords = open('data/stopwords.txt', encoding='utf-8').read().split('\n') stylecloud.gen_stylecloud(file_path='data/股市.csv', font_path='data/SourceHanSansCN-Regular.otf', output_name='output/股市2.png', icon_name='fas fa-question-circle', size=500, custom_stopwords=stopwords)
往期文章
公众号后台回复关键词【stylecloud】即可下载本文数据代码
“ 分享 ”和“ 在看 ”是更好的支持!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK