使用Python绘制词云图片过程如下:
在官网下载一个Python的安装包。首先,查看自己电脑是32位还是64位。针对不同的电脑选择不同的版本。
64位地址为:https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe
32位地址位:https://www.python.org/ftp/python/3.9.0/python-3.9.0.exe
下载完成后,按照提示进行安装。路径选择在D盘中。路径为D:\Python
python 安装完成后需要安装两个库文件,jieba和wordcloud。
然后,使用快捷键win+R打开运行,输入cmd
输入pip install jieba,就可以安装jieba库了。这里如果出现时间较长的问题可以使用命令pip install jieba --index https://pypi.mirrors.ustc.edu.cn/simple 进行安装。这里将安装的源设置为清华镜像资源,速度会有很大提升。
同理,安装wordcloud库也是如此。使用命令pip install wordcloud --index https://pypi.mirrors.ustc.edu.cn/simple
以此类推,如果想要安装某个库,就需要将库的名字改动一下就行了pip install (库的名字) --index https://pypi.mirrors.ustc.edu.cn/simple,镜像源网站可以从下面的网站里选择:
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
也就是:pip install (库的名字) --index (源网址)
可能出现的问题:
1.,如果输入pip install (库的名字) --index (源网址)后显示
那么就是pip的路径没有添加到环境变量path中,pip的路径一般在安装目录下面的Scripts目录。例如我的是D:\Python\Scripts
将pip的路径“F:\Install\python3.7\Scripts”添加到环境变量path中。
我们在cmd命令提示符中输入“pip --version”,按回车键查看一下版本信息,出现下图所示界面,代表环境变量配置成功。
也可以直接调用pip路径。在cmd命令符中,如图输入命令行,再执行pip install (库的名字) --index (源网址)即可。
如果在安装过程中,第一次安装可以执行,第二次不可以执行,报错提示信息为:ModuleNotFoundError: No module named ‘pip’,那么执行如下两条语句,即可解决问题:
python -m ensurepip
python -m pip install --upgrade pip
好了安装完成了我们来测试一下:
测试案例代码引用于博客,原文链接,侵删:3分钟教你用python制作一个简单词云 - Python探索牛 - 博客园
import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud
# 1.读入txt文本数据
text = open(r'test.txt', "r").read()
#print(text)
# 2.结巴中文分词,生成字符串,默认精确模式,如果不通过分词,无法直接生成正确的中文词云
cut_text = jieba.cut(text)
# print(type(cut_text))
# 必须给个符号分隔开分词结果来形成字符串,否则不能绘制词云
result = " ".join(cut_text)
#print(result)
# 3.生成词云图,这里需要注意的是WordCloud默认不支持中文,所以这里需已下载好的中文字库
# 无自定义背景图:需要指定生成词云图的像素大小,默认背景颜色为黑色,统一文字颜色:mode='RGBA'和colormap='pink'
wc = WordCloud(
# 设置字体,不指定就会出现乱码
# 设置背景色
background_color='white',
# 设置背景宽
width=500,
# 设置背景高
height=350,
# 最大字体
max_font_size=50,
# 最小字体
min_font_size=10,
mode='RGBA'
#colormap='pink'
)
# 产生词云
wc.generate(result)
# 保存图片
wc.to_file(r"wordcloud.png") # 按照设置的像素宽高度保存绘制好的词云图,比下面程序显示更清晰
# 4.显示图片
# 指定所绘图名称
plt.figure("jay")
# 以图片的形式显示词云
plt.imshow(wc)
# 关闭图像坐标系
plt.axis("off")
plt.show()
以下是test.txt 文件中的内容。里面的内容可以随意更改,但是由于默认的是英文,所以不要出现中文字符。
python python3 is good well bestbast shell cool
Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the
same day; May be backto oneself the paranoid weird belief disillusionment, these days, my mind has been very messy, in my mind constantly. Always
feel oneself should go to do something, or write something. Twenty years of life trajectory deeply shallow, suddenly feel something, do it.The end
of our life, and can meet many things really do?During myhood, think lucky money and new clothes are necessary for New Year, but as the advance of
the age, will be more and more found that those things are optional; Junior high school, thought to have a crush on just means that the real growth,
but over the past three years later, his writing of alumni in peace, suddenly found that isn't really grow up, it seems is not so important; Then
in high school, think don't want to give vent to out your inner voice can be in the high school children of the feelings in a period, but was event
ually infarction when graduation party in the throat, later again stood on the pitch he has sweat profusely, looked at his thrown a basketball hoops
, suddenly found himself has already can't remember his appearance.
运行效果如图所示:
如有错误,欢迎各位大佬批评指正。