根据文本生成三国演义的词云图,过滤掉单个字的词,具体代码如下:
import jieba
import wordcloud
from imageio import imread #引用imread()函数
mask=imread("D:/python数据分析/chinamap.jpg")
excludes={}
f=open("D:/python数据分析/threekingdoms.txt","r",encoding="utf-8")
t=f.read()
f.close()
ls=[x for x in jieba.lcut(t) if len(x)>=2] #过滤单个字符
txt=" ".join(ls)
w=wordcloud.WordCloud(
width=1000,height=700,
background_color="white",
font_path="msyh.ttc",mask=mask)
w.generate(txt)
w.to_file("D:/python数据分析/三国词云11.jpg")
运行结果如下:



