码迷,mamicode.com
首页 > 编程语言 > 详细

python绘制WordCloud词云图

时间:2020-02-15 18:44:02      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:ESS   a long   源泉   关键词   info   The   关系   max   cut   

前言

当我们想快速了解书籍、小说、电影剧本中的内容时,可以绘制 WordCloud 词云图,显示主要的关键词(高频词),可以非常直观地看到结果

核心代码

from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
from PIL import Image
import numpy as np

# 生成词云函数
def create_word_cloud(words):
     # 使用结巴分词
     text = " ".join(jieba.cut(words,cut_all=False, HMM=True))
     wc = WordCloud(
           font_path="./wc.ttf",
           max_words=100,
           width=2000,
           height=1200,
    )
     wordcloud = wc.generate(text)
     # 写词云图片
     wordcloud.to_file("wordcloud.jpg")
     # 显示词云文件
     plt.imshow(wordcloud)
     plt.axis("off")
     plt.show()

测试

ok,现在我们来传入一段文字,生成词云图片分析一下

s= """
1.life lies in movement. 生命在于运动
2.sport is the source of all life. 运动是生命的源泉.
3.to keep on, day after day practice go down, and only activities to keep the enthusiasm of adequate training and improve motor skills.
日复一日地坚持练下去吧,只有活动适量才能保持训练的热情和提高运动的技能.——塞涅卡
4.activity is the basis of life! 活动是生活的基础!——歌德
5.people's sound, not only by foods, especially to rely on motion.人的健全,不但靠饮食,尤靠运动.
6.the olympic motto is "higher, faster, stronger." 奥林匹克的格言是“更高,更快,更强”.
7.the health of the body for motionless and destruction, for sports practice and keep for a long time. --socrates 身体的健康因静止不动而破坏,因运动练习而长期保持.——苏格拉底
chenqionghe
chenqionghe
chenqionghe
chenqionghe
chenqionghe
muscle
muscle
muscle
muscle
muscle
yeah buddy! Light weight baby
yeah buddy! Light weight baby
yeah buddy! Light weight baby
chenqionghe, go to the gym,yeah buddy! Light weight baby
I was in the gym lifting weights.
"""
create_word_cloud(s)

运行结果如下
技术图片

分析这张图片,不难看出:chenqionghe喜欢运动,有肌肉,和light weight baby几个词关系密切

python绘制WordCloud词云图

标签:ESS   a long   源泉   关键词   info   The   关系   max   cut   

原文地址:https://www.cnblogs.com/chenqionghe/p/12312934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!