Python生成定制图片词云并打印出词频

代码:

import matplotlib.pyplot as plt

from wordcloud import WordCloud

import jieba

from scipy.misc import imread

import matplotlib

matplotlib.style.use('ggplot')

%matplotlib inline

#读取中文文本文件,生成词云图。

text_from_file_with_apath =open(r'E:\img\wordcloud\boe.txt',encoding='utf-8').read()

wordlist_after_jieba =jieba.cut(text_from_file_with_apath, cut_all = True)

wl_space_split = "".join(wordlist_after_jieba)

#逐行读取中文文本文件,生成以上词云词频统计列表,包含:关键字,该关键字出现的频率。

word_list = []

key_list=[]

for line in

open(r'E:\img\wordcloud\boe.txt',encoding='utf-8'):#1.txt是需要分词统计的文档

item = line.strip('\n\r').split('\t') #制表格切分

tags = jieba.analyse.extract_tags(item[0]) #jieba分词

for t in tags:

word_list.append(t)

word_dict= {}

orderList ={}

#print(word_list)

for item in word_list:

if item not in word_dict: #统计数量

word_dict[item] = 1

else:

word_dict[item] += 1

orderList=list(word_dict.values())

orderList.sort(reverse=True)

print(word_dict)

matplotlib.rcParams['figure.figsize'] =(20.0, 10.0)

color_mask =

imread("E:\img\wordcloud\zhanl.jpeg") #读取背景图片

my_wordcloud =WordCloud(font_path="E:\wuqiang\salestool\python\wordcloud\DroidSansFallbackFull.ttf"

, mask=color_mask,background_color="black", prefer_horizontal=1.0,width=1800,height=800, max_words=200).fit_words(word_dict)

#wordcloud = wordcloud

plt.imshow(my_wordcloud)

plt.axis("off")

plt.show()


结果:


Python生成定制图片词云并打印出词频_第1张图片


Python生成定制图片词云并打印出词频_第2张图片

你可能感兴趣的:(Python生成定制图片词云并打印出词频)