生成云图

#Author:Alex.Zhang
from wordcloud import WordCloud
with open("2.txt" ,encoding="utf-8")as file:
    #1.读取文本内容
    text=file.read()
    #2.设置词云的背景颜色、宽高、字数
    wordcloud=WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",
    background_color="black",width=600,
    height=300,max_words=50).generate(text)
    #3.生成图片
    image=wordcloud.to_image()
    #4.显示图片
    image.show()
# from wordcloud import WordCloud
# import jieba
# #1.将字符串切分为单个字符
# def chinese_jieba(text):
#     wordlist_jieba=jieba.cut(text)
#     space_wordlist=''.join(wordlist_jieba)
#     return  space_wordlist
# with open("test.txt" ,encoding="utf-8")as file:
#     text=file.read()
#     text=chinese_jieba(text)
#     wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",
#                           background_color="black", width=600,
#                           height=300, max_words=50,min_font_size=8).generate(text)
#     image=wordcloud.to_image()
#     image.show()
原文地址:https://www.cnblogs.com/klausage/p/11621810.html