词云

# -*- coding: utf-8 -*-
"""
Created on Mon Apr  6 22:45:36 2020

@author: 49594
"""

# coding:utf-8
 
 
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
 
 
# 生成词云
def create_word_cloud(filename):
    
    text = open("{}.txt".format(filename), 'rb' ).read()

    
    
    wordlist = jieba.cut(text, cut_all=True) # 结巴分词
    wl = " ".join(wordlist)
 
    # 设置词云
    wc = WordCloud(
        # 设置背景颜色
        background_color="black",
        # 设置最大显示的词云数
        max_words=2000,
        # 这种字体都在电脑字体中,一般路径
        font_path='simsun.ttf',
        height=1200,
        width=1600,
        # 设置字体最大值
        max_font_size=100,
        # 设置有多少种随机生成状态,即有多少种配色方案
        random_state=100,
    )
 
    myword = wc.generate(wl)  # 生成词云
    # 展示词云图
    plt.imshow(myword)
    plt.axis("off")
    plt.show()
    wc.to_file('img_book.png')  # 把词云保存下
 
 
if __name__ == '__main__':
    create_word_cloud('三国演义')
    

  

原文地址:https://www.cnblogs.com/leeing/p/12650437.html