1、读取中文文件、精准切分文章,切分后是生成器类型,利用.join()函数转换为str类型
f = open(u\'txt/AliceEN.txt\',\'r\').read() wordlist = jieba.cut(f, cut_all=False) word_string = \" \".join(wordlist)
2、配置词云字体,如果不配置font_path,中文字体会变成方框。\'txt/simsun.ttf\'代表本项目目录下的txt文件夹下的simsun.ttf字体。simsun.ttf字体可以去百度下载
wordcloud = WordCloud(font_path=\'txt/simsun.ttf\', background_color=\"white\",width=1000, height=860, margin=2).generate(word_string)
3显示 import matplotlib.pyplot as plt plt.imshow(wordcloud) plt.axis(\"off\") plt.show()
4、完整代码
# -*- coding: utf-8 -*- from wordcloud import WordCloud import jieba import os f = open(u\'txt/AliceEN.txt\',\'r\').read() wordlist = jieba.cut(f, cut_all=False) word_string = \" \".join(wordlist) wordcloud = WordCloud(font_path=\'txt/simsun.ttf\', background_color=\"white\",width=1000, height=860, margin=2).generate(word_string) import matplotlib.pyplot as plt plt.imshow(wordcloud) plt.axis(\"off\") plt.show() wordcloud.to_file(\'test.png\')
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。




