用Python做一个词云

首先用Pycharm安装两个包:wordcloud和imageio

File–>Settings–>Project Homework然后再看到下图点击红色框框的加号
在这里插入图片描述
然后出现下图,再搜索框搜索wordcloud,然后在左下角那里有个InstallPackage点击等待完成就行,按相同步骤搜索安装imageio就行了
在这里插入图片描述

安装好后在Pycharm输入代码

string = '''There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.
May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.
The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people
who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in lifeuntil you let go of your past failures and heartaches.
When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smiling and everyone around you is crying.
Please send this message to those people who mean something to you,to those who have touched your life in one way or another,to those who make you smile when you really need it,to those that make you see the brighter side of things when you are really down,to those who you want to
let them know that you appreciate their friendship. And if you don’t, don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.'''
from wordcloud import WordCloud
import imageio
prossed_string=string.replace(r'\W',' ')#将上面的string处理,用空格代替各种标点符号,这是正则的用法,匹配非字母和非数字,r是取消转义
bg_pic=imageio.imread('QQ.png')#QQ.png是网图,是你希望做成词云的模版
wordcloud = WordCloud(mask=bg_pic , background_color='black').generate(prossed_string)
wordcloud.to_file('1.jpg')#1.jpg这是生成的词云效果图,运行程序后自动生成,你随意命名,这里我命名为1.jpg
image_produce = wordcloud.to_image()
image_produce.show()

我这个py文件名为统计单词数量.py,在同一目录下还要有素材
在这里插入图片描述代码里有注释,大家看一下
准备好后运行这个py文件就会自动生成名为1.jpg的图片了
在这里插入图片描述
现在文件夹里多了个文件
在这里插入图片描述
这样就完成了!觉得有帮助的小伙伴点个赞哦~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值