隻用Python就可以制作的簡單詞雲

一、準備

詞雲制作所必需的三個包:

安裝:pip install matplotlib
安裝:pip install jieba
安裝:pip install wordcloud

(可能還需要其他包:pip install numpy等等,可以自行添加)

二、英文詞雲

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.

源代碼:

import matplotlib.pyplot as plt  # 在任何繪圖之前,我們需要一個figure對象,可以理解成我們需要一張畫板才能開始繪圖
import jieba  # jieba庫是中文分詞的第三方庫(中文文本需要通過分詞獲得單個的詞語)
from wordcloud import WordCloud  # 導入wordcloud庫

text = open(r'test.txt', "r").read()  # 讀入txt文本數據,在字符串前面加上字符r或R之後表示原始字符串,字符串中的任意字符都不再進行轉義,後一個r表示“隻讀”
cut_text = jieba.cut(text)  # 結巴中文分詞,生成字符串,默認精確模式,如果不通過分詞,無法直接生成正確的中文詞雲
result = " ".join(cut_text)  # 必須給個符號分隔開分詞結果來形成字符串,否則不能繪制詞雲
# join函數的用法:'sep'.join(seq)參數說明:sep:分隔符。可以為空;seq:要連接的元素序列、字符串、元組、字典;即:以sep作為分隔符,將seq所有的元素合並成一個新的字符串

# 生成詞雲圖,這裡需要註意的是WordCloud默認不支持中文,所以這裡需已下載好的中文字庫
# 無自定義背景圖:需要指定生成詞雲圖的像素大小,默認背景顏色為黑色,統一文字顏色:mode='RGBA'和colormap='pink'
wc = WordCloud(
    # 設置字體,不指定就會出現亂碼
    background_color='white',  # 設置背景色,默認為黑色
    width=500,  # 設置背景寬
    height=350,  # 設置背景高
    max_font_size=50,  # 最大字體
    min_font_size=10,  # 最小字體
    mode='RGBA'  # 當參數為“RGBA”並且background_color不為空時,背景為透明
)
wc.generate(result)  # 根據分詞後的文本產生詞雲
wc.to_file(r"wordcloud.png")  # 保存繪制好的詞雲圖
plt.imshow(wc)  # 以圖片的形式顯示詞雲
plt.axis("off")  # 關閉圖像坐標系,即不顯示坐標系
plt.show()  # plt.imshow()函數負責對圖像進行處理,並顯示其格式,但是不能顯示。其後必須有plt.show()才能顯示

效果展示:

在這裡插入圖片描述

三、中文詞雲

pkq.jpg模板內容:

在這裡插入圖片描述

poem.txt文本內容:

君不見黃河之水天上來,奔流到海不復回。
君不見高堂明鏡悲白發,朝如青絲暮成雪。
人生得意須盡歡,莫使金樽空對月。
天生我材必有用,千金散盡還復來。
烹羊宰牛且為樂,會須一飲三百杯。
岑夫子,丹丘生,將進酒,杯莫停。
與君歌一曲,請君為我側耳聽。
鐘鼓饌玉不足貴,但願長醉不復醒。
古來聖賢皆寂寞,惟有飲者留其名。
陳王昔時宴平樂,鬥酒十千恣歡謔。
主人何為言少錢,徑須沽取對君酌。
五花馬,千金裘,
呼兒將出換美酒,與爾同銷萬古愁。
床前明月光,疑是地上霜。
舉頭望明月,低頭思故鄉。
海客談瀛洲,煙濤微茫信難求。
越人語天姥,雲霞明滅或可睹。
天姥連天向天橫,勢拔五嶽掩赤城。
天臺四萬八千丈,對此欲倒東南傾。
我欲因之夢吳越,一夜飛度鏡湖月。
湖月照我影,送我至剡溪。
謝公宿處今尚在,淥水蕩漾清猿啼。
腳著謝公屐,身登青雲梯。
半壁見海日,空中聞天雞。
千巖萬轉路不定,迷花倚石忽已暝。
熊咆龍吟殷巖泉,栗深林兮驚層巔。
雲青青兮欲雨,水澹澹兮生煙。
列缺霹靂,丘巒崩摧。洞天石扉,訇然中開。
青冥浩蕩不見底,日月照耀金銀臺。
霓為衣兮風為馬,雲之君兮紛紛而來下。
虎鼓瑟兮鸞回車,仙之人兮列如麻。
忽魂悸以魄動,恍驚起而長嗟。
惟覺時之枕席,失向來之煙霞。
世間行樂亦如此,古來萬事東流水。
別君去兮何時還,且放白鹿青崖間,
須行即騎訪名山。安能摧眉折腰事權貴,
使我不得開心顏

源代碼:

import wordcloud
import numpy as np
from PIL import Image # Image模塊是在Python PIL圖像處理常用的模塊
import jieba

pic = Image.open("pkq.jpg")  # 打開圖片路徑,形成輪廓
shape = np.array(pic)  # 圖像輪廓轉換為數組
wc = wordcloud.WordCloud(mask=shape, font_path="simkai.ttf", background_color="white",
                         max_font_size=100)  # mask為圖片背景,font_path為字體,若不設置可能亂碼

text = open(r'poem.txt', "r", encoding='UTF-8').read()  # 對中文應該設置編碼方式為utf—8
cut_text = jieba.cut(text)
result = " ".join(cut_text)
wc.generate(result)
wc.to_file("cloud.jpg")

效果圖:

在這裡插入圖片描述

到此這篇關於隻用Python就可以制作的簡單詞雲的文章就介紹到這瞭,更多相關Python制作詞雲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: