python词云图

python词云图_第1张图片
图片发自App

强大的word cloud库,还可以自定义背景图片。

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import pickle
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
import jieba
 
with open('HotelComments.txt') as fin:
     for line in fin.readlines():
         line = line.strip('\n')
         text += ' '.join(jieba.cut(line))
         text += ' '
 fout = open('text.txt','wb')
 pickle.dump(text,)
 fout.close()

fr = open('text.txt','rb')
text = pickle.load(fr)  # wordcloud必须传入二进制文件

backgroud_Image = plt.imread('girl.jpg')
wc = WordCloud( background_color = 'white',    # 设置背景颜色
                mask = backgroud_Image,        # 设置背景图片
                max_words = 2000,            # 设置最大现实的字数
                stopwords = STOPWORDS,        # 设置停用词
                font_path = 'C:/Users/Windows/fonts/msyh.ttf',# 设置字体格式,如不设置显示不了中文
                max_font_size = 50,            # 设置字体最大值
                random_state = 30,            # 设置有多少种随机生成状态,即有多少种配色方案
                )
wc.generate(text)
image_colors = ImageColorGenerator(backgroud_Image)
wc.recolor(color_func = image_colors)
plt.imshow(wc)
plt.axis('off')
plt.show()

你可能感兴趣的:(python词云图)