python 绘画词云图

from wordcloud import WordCloud
import jieba
import numpy
import PIL.Image as Image
#1.将字符串切分
def chinese_jieba(text):
    wordlist_jieba=jieba.cut(text)
    space_wordlist=" ".join(wordlist_jieba)
    return space_wordlist
with open("1.txt" ,encoding="utf-8")as file:
    text=file.read()
    text=chinese_jieba(text)
    mask_pic=numpy.array(Image.open("chinamap.png"))
    #3.将参数mask设值为:mask_pic
    wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",mask=mask_pic,background_color="white").generate(text)
    image=wordcloud.to_image()
    image.show()

猜你喜欢

转载自blog.csdn.net/weixin_52797843/article/details/122547594