|
发表于 2021-2-24 13:18:13
|
显示全部楼层
- import jieba
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- from wordcloud import WordCloud
- from PIL import Image
- #定义一个空字符串
- final = ""
- #文件夹位置
- filename = r"C:\Users\86189\ccc\wordabc.txt"
- #打开文件夹,读取内容,并进行分词
- with open(filename,'r',encoding = 'gb2312') as f:
- for line in f.readlines():
- word = jieba.cut(line)
- for i in word:
- final = final + i +" "
- print(final)
- word_pic = WordCloud(font_path = r"C:\\windows\\Fonts\\simhei.ttf",width = 2000,height = 1000).generate(final)
- plt.imshow(word_pic)
- # 在只设置mask的情况下 会得到一个拥有图片形状的词云 axis默认为on 会开启边框
- plt.imshow(word_pic, interpolation="bilinear")
- plt.axis("off")
- plt.savefig("c.jpg")
复制代码 |
|