赞
踩
wordcloud是优秀的词云展示的第三方库
词云以词语为基本单位,更加直观和艺术的展示文本。
pip install wordcloud
wordcloud库把词云当作一个WordCloud对象
w = wordcloud.WordCloud()
示例
import wordcloud
c = wordcloud.WordCloud()
c.generate("wordcloud by python")
c.to_file("pywordcloud.png")
从文本到图片,wordcloud做了哪些事情呢?
w = wordcloud.WordCloud(<参数>)
#指定词云形状,默认为长方形,需要引用imread()函数
from scipy.misc import imread
mk = imread("pic.png")
w = wordcloud.WordCloud(mask = mk)
示例一
import wordcloud
txt = "life is short, you need python"
w = wordcloud.WordCloud( \
background_color = "white")
w.generate(txt)
w.to_file("d:/pywordcloud.png")
示例二
import jieba
import wordcloud
txt = "程序设计语言是计算机能够理解和\
识别用户操作意图的一种交互体系,它按照\
特定规则组织计算机指令,使计算机能够自\
动进行各种运算处理。"
w = wordcloud.WordCloud( width = 1000, \
font_path = "msyh.ttc", height = 700)
w.generate(" ".join(jieba.lcut(txt)))
w.to_file("d:/zhongwenciyun.jpg")
直观理解政策文件
示例一
#GovRptWordCloudv1.py
import jieba
import wordcloud
f = open("关于实施乡村振兴战略的意见.txt", "r", encoding="utf-8")
#f = open("新时代中国特色社会主义.txt","r",encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = "".join(ls)
w = wordcloud.WordCloud(font_path = "msyh.ttc", width = 1000, height = 700, background_color = "white",)
w.generate(txt)
w.to_file("grwordcloud.png")
示例二(增加参数max_words=15)
#GovRptWordCloudv2.py
import jieba
import wordcloud
f = open("关于实施乡村振兴战略的意见.txt", "r", encoding="utf-8")
#f = open("新时代中国特色社会主义.txt","r",encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = "".join(ls)
w = wordcloud.WordCloud(max_words = 15, font_path = "msyh.ttc", width = 1000, height = 700, background_color = "white")
w.generate(txt)
w.to_file("grwordcloud.png")
示例三(增加形状mask = mask)
#GovRptWordCloudv3.py
import jieba
import wordcloud
from scipy.misc import imread
mask = imread("fivestart.png")
f = open("关于实施乡村振兴战略的意见.txt", "r", encoding="utf-8")
#f = open("新时代中国特色社会主义.txt","r",encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = "".join(ls)
w = wordcloud.WordCloud(font_path = "msyh.ttc",mask = mask, width = 1000, height = 700, background_color = "white")
w.generate(txt)
w.to_file("grwordcloud.png")
扩展能力
链接: 关于实施乡村振兴战略的意见.txt
链接: 新时代中国特色社会主义.txt
链接: chinamap.jpg
链接: fivestars.png
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。