赞
踩
- import jieba
- from wordcloud import WordCloud
- import matplotlib.pyplot as plt
- from matplotlib import colors
- from PIL import Image
- import numpy as np
-
- # 导入文本数据进行简单的文本处理,去掉换行符,半角和全角空格
- f = open("bb.txt","r", encoding='utf8')
- t = f.read()
- f.close()
-
-
- ls = jieba.lcut(t)
- txt = " ".join(ls)
-
-
- # 导入停词,用于去掉文本中类似于'啊'、'你','我'之类的词
- stop_words = open("bb.txt", encoding="utf8").read().split("\n")
-
-
- # 导入背景图,注意背景图除了目标形状外,其余地方都应是空白的
- background_image = np.array(Image.open("5.jpg"))
-
- color_list=["#C2D26F","#C848A3","#B22222","#B03060","#EEA9B8","#708090"]
- colormap=colors.ListedColormap(color_list)
- # 使用WordCloud生成词云
- w = WordCloud(font_path="simsun.ttc", # 设置词云字体
- background_color="white", width=1000, height=880, # 词云图的背景颜色、高度和宽度
- mask=background_image, # 指定词云的形状
- max_words=1500, # 显示单词数
- max_font_size=100, # 最大字号
- colormap=colormap,
- stopwords=stop_words) # 去掉的停词
- w.generate(txt)
-
- # 运用matplotlib展现结果
- plt.subplots(figsize=(10, 8))
- plt.imshow(w)
- plt.axis("off")
- plt.show()

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。