官方最簡單的例子:
from os import path
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()
查看官方所有例子
需求:
- 文字統(tǒng)一顏色样漆。
- 透明背景胧辽。
- 按照指定樣式顯示汛骂。
要求所有顏色為#FFF0D0
眼虱,使用在線工具將RGB轉換為hsl格式:hsl(40.9,100%,90.8%)
侦香,因為WordCloud API hsl值為浮點數(shù)時異常刻获,因此改為整數(shù)hsl(41,100%,91%)
罐脊。
統(tǒng)一文字顏色
WordCloud(mode='RGBA', colormap='pink')
當mode
設置為時RGBA
時承绸,使用colormap
色值褐望,參數(shù)的值可以從文檔中或者報錯信息中得知勒庄。
背景透明
WordCloud(background_color=None)
參考: