一、準(zhǔn)備
詞云制作所必需的三個(gè)包:
安裝:pip install matplotlib
安裝:pip install jieba
安裝:pip install wordcloud
(可能還需要其他包:pip install numpy等等,可以自行添加)
二、英文詞云
test.txt文本內(nèi)容:
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.
源代碼:
import matplotlib.pyplot as plt # 在任何繪圖之前,我們需要一個(gè)figure對(duì)象,可以理解成我們需要一張畫(huà)板才能開(kāi)始繪圖
import jieba # jieba庫(kù)是中文分詞的第三方庫(kù)(中文文本需要通過(guò)分詞獲得單個(gè)的詞語(yǔ))
from wordcloud import WordCloud # 導(dǎo)入wordcloud庫(kù)
text = open(r'test.txt', "r").read() # 讀入txt文本數(shù)據(jù),在字符串前面加上字符r或R之后表示原始字符串,字符串中的任意字符都不再進(jìn)行轉(zhuǎn)義,后一個(gè)r表示“只讀”
cut_text = jieba.cut(text) # 結(jié)巴中文分詞,生成字符串,默認(rèn)精確模式,如果不通過(guò)分詞,無(wú)法直接生成正確的中文詞云
result = " ".join(cut_text) # 必須給個(gè)符號(hào)分隔開(kāi)分詞結(jié)果來(lái)形成字符串,否則不能繪制詞云
# join函數(shù)的用法:'sep'.join(seq)參數(shù)說(shuō)明:sep:分隔符??梢詾榭?;seq:要連接的元素序列、字符串、元組、字典;即:以sep作為分隔符,將seq所有的元素合并成一個(gè)新的字符串
# 生成詞云圖,這里需要注意的是WordCloud默認(rèn)不支持中文,所以這里需已下載好的中文字庫(kù)
# 無(wú)自定義背景圖:需要指定生成詞云圖的像素大小,默認(rèn)背景顏色為黑色,統(tǒng)一文字顏色:mode='RGBA'和colormap='pink'
wc = WordCloud(
# 設(shè)置字體,不指定就會(huì)出現(xiàn)亂碼
background_color='white', # 設(shè)置背景色,默認(rèn)為黑色
width=500, # 設(shè)置背景寬
height=350, # 設(shè)置背景高
max_font_size=50, # 最大字體
min_font_size=10, # 最小字體
mode='RGBA' # 當(dāng)參數(shù)為“RGBA”并且background_color不為空時(shí),背景為透明
)
wc.generate(result) # 根據(jù)分詞后的文本產(chǎn)生詞云
wc.to_file(r"wordcloud.png") # 保存繪制好的詞云圖
plt.imshow(wc) # 以圖片的形式顯示詞云
plt.axis("off") # 關(guān)閉圖像坐標(biāo)系,即不顯示坐標(biāo)系
plt.show() # plt.imshow()函數(shù)負(fù)責(zé)對(duì)圖像進(jìn)行處理,并顯示其格式,但是不能顯示。其后必須有plt.show()才能顯示
效果展示:
![](/d/20211017/96315b74411effdfb4f89b49f8b73571.gif)
三、中文詞云
pkq.jpg模板內(nèi)容:
![](/d/20211017/fc46e2f118156c5ca873f1d35713d1e3.gif)
poem.txt文本內(nèi)容:
君不見(jiàn)黃河之水天上來(lái),奔流到海不復(fù)回。
君不見(jiàn)高堂明鏡悲白發(fā),朝如青絲暮成雪。
人生得意須盡歡,莫使金樽空對(duì)月。
天生我材必有用,千金散盡還復(fù)來(lái)。
烹羊宰牛且為樂(lè),會(huì)須一飲三百杯。
岑夫子,丹丘生,將進(jìn)酒,杯莫停。
與君歌一曲,請(qǐng)君為我側(cè)耳聽(tīng)。
鐘鼓饌玉不足貴,但愿長(zhǎng)醉不復(fù)醒。
古來(lái)圣賢皆寂寞,惟有飲者留其名。
陳王昔時(shí)宴平樂(lè),斗酒十千恣歡謔。
主人何為言少錢(qián),徑須沽取對(duì)君酌。
五花馬,千金裘,
呼兒將出換美酒,與爾同銷(xiāo)萬(wàn)古愁。
床前明月光,疑是地上霜。
舉頭望明月,低頭思故鄉(xiāng)。
??驼勫?,煙濤微茫信難求。
越人語(yǔ)天姥,云霞明滅或可睹。
天姥連天向天橫,勢(shì)拔五岳掩赤城。
天臺(tái)四萬(wàn)八千丈,對(duì)此欲倒東南傾。
我欲因之夢(mèng)吳越,一夜飛度鏡湖月。
湖月照我影,送我至剡溪。
謝公宿處今尚在,淥水蕩漾清猿啼。
腳著謝公屐,身登青云梯。
半壁見(jiàn)海日,空中聞天雞。
千巖萬(wàn)轉(zhuǎn)路不定,迷花倚石忽已暝。
熊咆龍吟殷巖泉,栗深林兮驚層巔。
云青青兮欲雨,水澹澹兮生煙。
列缺霹靂,丘巒崩摧。洞天石扉,訇然中開(kāi)。
青冥浩蕩不見(jiàn)底,日月照耀金銀臺(tái)。
霓為衣兮風(fēng)為馬,云之君兮紛紛而來(lái)下。
虎鼓瑟兮鸞回車(chē),仙之人兮列如麻。
忽魂悸以魄動(dòng),恍驚起而長(zhǎng)嗟。
惟覺(jué)時(shí)之枕席,失向來(lái)之煙霞。
世間行樂(lè)亦如此,古來(lái)萬(wàn)事東流水。
別君去兮何時(shí)還,且放白鹿青崖間,
須行即騎訪(fǎng)名山。安能摧眉折腰事權(quán)貴,
使我不得開(kāi)心顏
源代碼:
import wordcloud
import numpy as np
from PIL import Image # Image模塊是在Python PIL圖像處理常用的模塊
import jieba
pic = Image.open("pkq.jpg") # 打開(kāi)圖片路徑,形成輪廓
shape = np.array(pic) # 圖像輪廓轉(zhuǎn)換為數(shù)組
wc = wordcloud.WordCloud(mask=shape, font_path="simkai.ttf", background_color="white",
max_font_size=100) # mask為圖片背景,font_path為字體,若不設(shè)置可能亂碼
text = open(r'poem.txt', "r", encoding='UTF-8').read() # 對(duì)中文應(yīng)該設(shè)置編碼方式為utf—8
cut_text = jieba.cut(text)
result = " ".join(cut_text)
wc.generate(result)
wc.to_file("cloud.jpg")
效果圖:
![](/d/20211017/c7191add4de13e59c09181611edc4ec1.gif)
到此這篇關(guān)于只用Python就可以制作的簡(jiǎn)單詞云的文章就介紹到這了,更多相關(guān)Python制作詞云內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python制作詞云的方法
- 用python制作詞云視頻詳解
- 使用python實(shí)現(xiàn)個(gè)性化詞云的方法
- python生成詞云的實(shí)現(xiàn)方法(推薦)
- 使用python生成云詞圖實(shí)現(xiàn)畫(huà)紅樓夢(mèng)詞云圖