1.参照北京理工大学mooc课程:python程序设计
https://www.icourse163.org/learn/BIT-268001?tid=1002788003&from=study#/learn/content
源代码为:
import jieba
import wordcloud
from scipy.misc import imread
mask = imread("chinamap.png")
f = open("十九大报告全文.txt", "r", encoding="gb18030")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(font_path="msyh.ttf", mask=mask, width = 1000 , height = 700,background_color="white")
w.generate(txt)
w.to_file("cy_1.png")
出现错误:
DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.
查阅资料之后安装了imagio库
代码如下:
import jieba
import wordcloud
from imageio import imread
mask = imread("chinamap.png")
f = open("十九大报告全文.txt", "r", encoding="gb18030")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(font_path="msyh.ttf", mask=mask, background_color="white")
w.generate(txt)
w.to_file("cy1.png")
输出:
注意:这个地方使用的中国全图图片,是直接百度得到的,但是需要注意地是:对于图片的背景有要求,原图是白色背景,发现生成的词云是正方形的,没有显示中国全景,所以利用ppt的背景填充功能:设置图片填充-纯色填充-透明度100%,另存为新的图片。
这样就完成了设计。