python实现词云

1、安装
使用命令【pip install wordcloud】安装词云

2、参数
使用了OpenCV的数据格式进行读取,字体能够多试几种python

def create_wordcloud_pic(): stopwords = get_noisy_word()  # 噪声词以字典的形式进行呈现
    img = cv2.imread(img_path)  # 图片矩阵
    font = r'C:\Windows\Fonts\simsun.ttc'  # 字体
    cloud = WordCloud( # 设置字体,不指定就会出现乱码
        font_path=font,  # 这个路径是pc中的字体路径
        # 设置背景色
        background_color='white', # 词云形状
        mask=img, # 容许最大词汇
        max_words=50, # 最大号字体
        max_font_size=100, # 过滤噪声词
        stopwords=stopwords, # 设置有多少种随机生成状态,即有多少种配色方案
        random_state=30, # 模糊度
        # scale=1
 ) word_cloud = cloud.generate_from_frequencies(content)  # 字典形式
    # word_cloud = cloud.generate(content) # 可视化字符 产生词云,输入的格式是以空格分隔的词语组成的字符串
    word_cloud.to_file("result.jpg")  # 保存图片
    # 显示词云图片
 plt.imshow(word_cloud) plt.axis('off') plt.show()


3、结果展现git

效果不是很好,还需改进
github

 

 

 
4、参考
OpenCV的包
https://www.lfd.uci.edu/~gohlke/pythonlibs/dom

 

官网
https://amueller.github.io/word_cloud/
字体

 

github
https://github.com/amueller/word_cloudspa

相关文章
相关标签/搜索