使用Python繪制詞云圖片過程如下:
在官網(wǎng)下載一個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
下載完成后汗贫,按照提示進(jìn)行安裝身坐。路徑選擇在D盤中秸脱。路徑為D:\Python
python 安裝完成后需要安裝兩個庫文件,jieba和wordcloud部蛇。
然后摊唇,使用快捷鍵win+R打開運(yùn)行,輸入cmd
輸入pip install jieba涯鲁,就可以安裝jieba庫了巷查。這里如果出現(xiàn)時間較長的問題可以使用命令pip install jieba --index https://pypi.mirrors.ustc.edu.cn/simple ? ? 進(jìn)行安裝。這里將安裝的源設(shè)置為清華鏡像資源撮竿,速度會有很大提升吮便。
同理,安裝wordcloud庫也是如此幢踏。使用命令pip install wordcloud --index https://pypi.mirrors.ustc.edu.cn/simple
以此類推髓需,如果想要安裝某個庫,就需要將庫的名字改動一下就行了pip install (庫的名字) --index https://pypi.mirrors.ustc.edu.cn/simple房蝉,鏡像源網(wǎng)站可以從下面的網(wǎng)站里選擇:
清華:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中國科技大學(xué)https://pypi.mirrors.ustc.edu.cn/simple/
華中理工大學(xué):http://pypi.hustunique.com/
山東理工大學(xué):http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
也就是:pip install (庫的名字) --index (源網(wǎng)址)
可能出現(xiàn)的問題:
1.僚匆,如果輸入pip install (庫的名字) --index (源網(wǎng)址)后顯示
那么就是pip的路徑?jīng)]有添加到環(huán)境變量path中,pip的路徑一般在安裝目錄下面的Scripts目錄搭幻。例如我的是D:\Python\Scripts
將pip的路徑“F:\Install\python3.7\Scripts”添加到環(huán)境變量path中咧擂。
我們在cmd命令提示符中輸入“pip --version”,按回車鍵查看一下版本信息檀蹋,出現(xiàn)下圖所示界面松申,代表環(huán)境變量配置成功。
也可以直接調(diào)用pip路徑俯逾。在cmd命令符中贸桶,如圖輸入命令行,再執(zhí)行pip install (庫的名字) --index (源網(wǎng)址)即可桌肴。
如果在安裝過程中皇筛,第一次安裝可以執(zhí)行,第二次不可以執(zhí)行坠七,報(bào)錯提示信息為:ModuleNotFoundError: No module named ‘pip’水醋,那么執(zhí)行如下兩條語句,即可解決問題:
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文本數(shù)據(jù)
text = open(r'test.txt', "r").read()
#print(text)
# 2.結(jié)巴中文分詞,生成字符串拳魁,默認(rèn)精確模式宫蛆,如果不通過分詞,無法直接生成正確的中文詞云
cut_text = jieba.cut(text)
# print(type(cut_text))
# 必須給個符號分隔開分詞結(jié)果來形成字符串,否則不能繪制詞云
result = " ".join(cut_text)
#print(result)
# 3.生成詞云圖,這里需要注意的是WordCloud默認(rèn)不支持中文耀盗,所以這里需已下載好的中文字庫
# 無自定義背景圖:需要指定生成詞云圖的像素大小想虎,默認(rèn)背景顏色為黑色,統(tǒng)一文字顏色:mode='RGBA'和colormap='pink'
wc = WordCloud(
? ? ? ? # 設(shè)置字體,不指定就會出現(xiàn)亂碼
? ? ? ? # 設(shè)置背景色
? ? ? ? background_color='white',
? ? ? ? # 設(shè)置背景寬
? ? ? ? width=500,
? ? ? ? # 設(shè)置背景高
? ? ? ? height=350,
? ? ? ? # 最大字體
? ? ? ? max_font_size=50,
? ? ? ? # 最小字體
? ? ? ? min_font_size=10,
? ? ? ? mode='RGBA'
? ? ? ? #colormap='pink'
? ? ? ? )
# 產(chǎn)生詞云
wc.generate(result)
# 保存圖片
wc.to_file(r"wordcloud.png") # 按照設(shè)置的像素寬高度保存繪制好的詞云圖叛拷,比下面程序顯示更清晰
# 4.顯示圖片
# 指定所繪圖名稱
plt.figure("jay")
# 以圖片的形式顯示詞云
plt.imshow(wc)
# 關(guān)閉圖像坐標(biāo)系
plt.axis("off")
plt.show()
以下是test.txt 文件中的內(nèi)容舌厨。里面的內(nèi)容可以隨意更改,但是由于默認(rèn)的是英文忿薇,所以不要出現(xiàn)中文字符裙椭。
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.
運(yùn)行效果如圖所示:
如有錯誤,歡迎各位大佬批評指正署浩。