云图(wordcloud)
-
在一些数据分析的可视化展示中,经常会看到用云图展示的方式。如下便是一个的基于文本分析的简单云图展示demo
# 导入必要的包 import matplotlib.pyplot as plt import wordcloud
text = open('dataset/reasonOfLoan.txt').read()
问题: 'gbk' codec can't decode byte 0xa6 in position 11963512: illegal multibyte sequence
解决:file = open(path, 'rb')
text = open('dataset/reasonOfLoan.txt','rb').read().decode('utf-8')
wordcloud.WordCloud().generate(text)
问题:cannot use a string pattern on a bytes-like object
wordcloud.STOPWORDS.add('consolidation')
wordcloud.STOPWORDS.add('debt_consolidation')
wordcloud.STOPWORDS.add('Debt')
wordcloud.STOPWORDS.add('refinancing')
wordcloud.STOPWORDS.add('br')
wordcloud.STOPWORDS.add('added')
wcd = wordcloud.WordCloud(
background_color='white', # 背景颜色
max_words=1000, # 最大词数
# mask=back_color, # 以该参数值作图绘制词云,这个参数不为空时,width和height会被忽略
max_font_size=100, # 显示字体的最大值
stopwords=wordcloud.STOPWORDS.add('Borrower'), # 使用内置的屏蔽词,再添加'苟利国'
# font_path="C:/Windows/Fonts/STFANGSO.ttf", # 解决显示口字型乱码问题,可进入C:/Windows/Fonts/目录更换字体
random_state=42, # 为每个词返回一个PIL颜色
width=1000, # 图片的宽
height=860 #图片的长
).generate(text)
plt.figure(figsize = (50, 50))
plt.axis('off')
plt.imshow(wcd)
- 效果图:
转载于:https://my.oschina.net/dwqdwd/blog/1811635