48

stylecloud:简洁易用的词云库

 3 years ago
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)

u6zmuur.png!web

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)

AFf6vmA.png!web

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 搜到才可以。

nuMrIr7.png!web

比如以 高考 为例,我会在搜索框搜索跟 教育 相关的关键词

  • 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)

bY3QRz7.png!web

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)

neyE32Z.png!web

往期文章

小案例: Pandas的apply方法 

用Python绘制近20年地方财政收入变迁史视频

Python语法快速入门

Python网络爬虫与文本数据分析

读完本文你就了解什么是文本分析

文本分析在经管领域中的应用概述

综述:文本分析在市场营销研究中的应用

从记者的Twitter关注看他们稿件的党派倾向?

Pandas时间序列数据操作

70G上市公司定期报告数据集

文本数据清洗之正则表达式

shreport库: 批量下载上海证券交易所上市公司年报

Numpy和Pandas性能改善的方法和技巧

漂亮~pandas可以无缝衔接Bokeh

YelpDaset: 酒店管理类数据集10+G

公众号后台回复关键词【stylecloud】即可下载本文数据代码

分享 ”和“ 在看 ”是更好的支持!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK