用Python轻松搞定词云图(高考英语作文+斗罗大陆)

引言

你是否在网络上经常看到各种各样的词云图呢?当你看到它们时,你有没有想一下,这些词云图是怎么生成的呢?其实,词云图的生成非常简单,用Python几行代码就可以轻松搞定词云图。这里分别以英语作文和《斗罗大陆》为例,给大家展示一下如何用Python生成词云图。

具体实现

首先,我们用英语作文来试一下,而英文与中文的区别就是英文语料是不需要进行分词的,因为,英文中,各个单词都是以空格进行分隔的。

Notice
Boys and girls,
May I have your attention, please? Recently, an outstanding movie will be shown in our campus. Welcome to join us end enjoy it! Here are some relevant details about it.
To begin with, the name of the movie is Growing together, which is about the development of our beloved school; as a result, it will be not only meaningful but also interesting. Besides, it will be in the library from 2:00 to 4:00 in the afternoon on June 9th . What’s more, everyone of you will be welcome to take part in it, enjoying the movie, having a heated discussion afterwards and giving your own comments.
Hopefully, you would make it to our activity. I have the confidence that you will have a great time.
The Student Union

这是一篇英语高考作文,我们来生成一下它的词云图。

# -*- coding: utf-8 -*-

from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 打开文本
text = open('./res/English.txt','r').read()

# 生成词云对象
wc = WordCloud().generate(text)

# 显示词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()


# 保存词云
wc.to_file('./result/wordcloud1.png')

生成的词云图如下:
在这里插入图片描述
接下来我们用中文试一下,这里以小说《斗罗大陆》为例。
在这里插入图片描述

# -*- coding: utf-8 -*-

from wordcloud import WordCloud 
import matplotlib.pyplot as plt 
import jieba

# 打开文本
text = open
  • 25
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值