import jieba
import wordcloud f = open("新时代中国特色社会主义.txt", "r", encoding="gbk") #只读.txt文件。这里一定注意是gbk!!!,而不是utf-8(如果出错,请把encoding删除)

t = f.read()
f.close()
ls = jieba.lcut(t) txt = " ".join(ls)
w = wordcloud.WordCloud(
width=1000, height=700,
background_color="white",
font_path="MSYH.ttc" #没有设置字体可能出现,词云的结果均为方框。建议设置MSYH.ttc/MSYH.TTC(微软雅黑)
)
w.generate(txt)
w.to_file("wordcloud1.png") #在程序当前目录,生成图片wordcloud1.png

wordcloud词云--可视化-LMLPHP

 import jieba
import wordcloud
from scipy.misc import imread mask = imread("chinamap.jpg") #设置背景图片chinamap.jpg
excludes = { } f = open("新时代中国特色社会主义.txt", "r", encoding="gbk") #只读.txt文件。这里一定注意是gbk!!!,而不是utf-8(如果出错,请把encoding删除)

t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls) w = wordcloud.WordCloud(
width = 550, height = 500,
background_color = "white",
font_path = "MSYH.ttc", mask = mask #没有设置字体可能出现,词云的结果均为方框。建议设置MSYH.ttc/MSYH.TTC(微软雅黑)
) w.generate(txt)
w.to_file("wordcloud2.png") #在程序当前目录,生成图片wordcloud2.png

wordcloud词云--可视化-LMLPHP

05-11 22:16