直接pip總是會報錯,基本放棄了,還是乖乖下載下傳wheel檔案吧
一個超好用的網站,所有的wheel檔案都可以找到
https://pypi.org/
wordcloud 詞雲必須要有的包
用法:
必須有一個字元串,也可以讀取一個檔案
from wordcloud import WordCloud
file2 = open('../test.txt', encoding='utf-8').read()
wordcloud = WordCloud(font_path='C:\Windows\Fonts/FZSTK.TTF', background_color="white", height=20000, width=20000).generate(file2)
wordcloud.to_file('test.png')
參數說明:
font_path:字型路徑(中文必須要有,否則全是方框),height:圖檔高度,width:圖檔寬度,generate的參數為要生成詞雲的文章
隻要這四行就可以生成一個矩形的詞雲

如果想生成指定圖形的詞雲,隻需要再導入PIL包和numpy包即可
如果是Python3,在上邊那個網站找pillow,安裝之後用法和PIL一樣
from PIL import Image
import numpy as np
打開一張圖檔,并将其轉換成數組
img = Image.open('巴黎聖母院.jpg')
img_array = np.array(img)
然後在WordCloud的構造方法中加上 mask參數,值為由圖檔轉換成的數組
wordcloud = WordCloud(font_path='C:\Windows\Fonts/FZSTK.TTF', background_color="white", height=20000, width=20000, mask=img_array).generate(file2)
全部代碼為
#coding = utf-8
from wordcloud import WordCloud
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
string = "Importance of relative word frequencies for font-size. With relative_scaling=0, only word-ranks are considered. With relative_scaling=1, a word that is twice as frequent will have twice the size. If you want to consider the word frequencies and not only their rank, relative_scaling around .5 often looks good."
file1 = open('../files/test.txt', encoding='utf-8').read()
file2 = open('../test.txt', encoding='utf-8').read()
img = Image.open('巴黎聖母院.jpg')
img_array = np.array(img)
"""font_path:字型路徑(中文必須要有,否則全是方框),height:圖檔高度,width:圖檔寬度,mask:背景圖檔數組,generate的參數為要生成詞雲的文章"""
wordcloud = WordCloud(font_path='C:\Windows\Fonts/FZSTK.TTF', background_color="white", height=2000, width=2000, mask=img_array).generate(file2)
"""将生成的詞雲儲存成圖檔"""
wordcloud.to_file('test.png')
"""plt可以不用"""
plt.imshow(wordcloud)
plt.show()