您的位置:首页 > 编程语言 > Python开发

如何用python制作词云--wordcloud

2020-06-04 06:43 531 查看

如何用python制作词云–wordcloud

由于第一次写CSDN博客,水平有限。写这份python制作词云,想帮助一些不太了解python但是想快速制作出词云的小伙伴。网上也有很多其他的方法,我简单做了一部分,用起来比较方便,感谢大家观看。

第一步 首先用python编辑你可以选择一个python的编辑器,然后做一点准备工作。要准备一段txt文档,,就是你想让它生产词云的文档。你可以选择一些语录、歌词、文章、留言等等。

英文词云

v1.0

首先在词云制作过程中,有英文和中文的区别,英文比较简单,中文需要更进一步。英文的编辑可以直接生成普通的词云。
我随便搜了一点英文文章,如下
aa bb cc rr fd gh ffe ed fdf hello water back forword name one two three thursday friday today hoop tell
proun Hooray! It’s snowing! It’s time to make a snowman.
James runs out. He makes a big pile of snow.
He puts a big snowball on top. He adds a scarf and a hat.
He adds an orange for the nose. He adds coal for the eyes and buttons.
In the evening, James opens the door. What does he see? The snowman is moving!
James invites him in. The snowman has never been inside a house.
He says hello to the cat. He plays with paper towels.
A moment later, the snowman takes James’s hand and goes out.
They go up, up, up into the air! They are flying! What a wonderful night!
The next morning, James jumps out of bed. He runs to the door.
He wants to thank the snowman. But he’s gone.
将这一段文字做成txt文件,放在程序的同一个文件夹下,方便直接调用。
代码如下:
下面展示一些

内联代码片


from wordcloud import WordCloud

def main():
"""
主函数

"""
# 1、读入txt文本数据
with open("Englishwordcloud.txt", 'r') as fp:
text = fp.read()
# print(text)

# 2、生成词云
wordcloud = WordCloud().generate(text)  # 将该文本生成词云
imag = wordcloud.to_image()
imag.show()  # 展示图片
imag.save('one.jpg')  # 图片保存的名称
print('图片保存成功')  # 提醒你图片已保存成功

if __name__ == '__main__':
main()

得到如图结果:


在这里我选用了pycharm作为编辑器,你们也可以使用该编辑方法。

V1.1

上面是属于最简单的方式,直接用wordcloud生成词云云,也有一些是用PIL的方式来画图,然后消除坐标轴,展示图片,我自己看过一点,大家有兴趣也可以试试。那么我们来再改进以下,使词云有形状,而不是简单的长方形。
代码如下:
下面展示一些

内联代码片


from PIL import Image
from wordcloud import WordCloud
import numpy as np

def main():
"""
主函数

"""
# 1、读入txt文本数据
with open("Englishwordcloud.txt", 'r', encoding='utf8') as fp:
text = fp.read()
# print(text)

# 2、生成词云

# 用numpy函数来将图片处理成词云的形状
mask = np.array(Image.open('wujiaoxing.jpg'))  # 选择一张你要的形状图,放在这里
# 增加mask
wordcloud = WordCloud(
mask=mask,

).generate(text)
imag = wordcloud.to_image()
imag.show()
imag.save('one.jpg')
print('图片保存成功')

if __name__ == '__main__':
main()

得到结果

中文

中文的话你如果直接用上面的代码,是显示不出文字的,会得到以下结果


中文生成词云,你还要准备jieba这一库,还要选择一种字体font。font要下载到你的这个程序的库里面。
具体代码如下:

下面展示一些

内联代码片


from PIL import Image
# 中文分词
import jieba
import numpy as np
from wordcloud import WordCloud

def main():
"""
主函数

"""
# 1、读入txt文本数据
with open("chinseswordcloud.txt", 'r', encoding='utf8') as fp:
text = fp.read().replace(' ','').replace('\n','').strip()
# print(text)

# 2、把词云剪开
text = jieba.cut(text)
text = ' '.join(text)  # 要有空格

# print(text)

mask = np.array(Image.open('wuhandaxue.jpg'))

# 3、生成词云图
wordcloud = WordCloud(

mask=mask,
font_path='STXINGKA.TTF',  # 字体路径,识别中文
background_color='white',  #背景颜色
#图片大小
width=1000,
height=650,
#字体大小
max_font_size=50,
min_font_size=10,

).generate(text)

#4、显示以及保存图片
imag = wordcloud.to_image()
imag.show()
imag.save('six.jpg')
print('图片保存成功')

if __name__ == '__main__':
main()

该部分代码作为初学者也可以看看。
选用不同的形状,可以得到不太结果。


就先到这。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: