WordCloud のサンプルです。
wordcloud01.py
# ! /usr/bin/python
#
# wordcloud01.py
#
# Apr/14/2019
# ------------------------------------------------------------------
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from wordcloud import WordCloud
# ------------------------------------------------------------------
def create_wordcloud(text,file_png):
fpath = "/usr/share/fonts/OTF/TakaoMincho.ttf"
# ストップワードの設定
stop_words = ['あ', 'い']
wordcloud = WordCloud(background_color="white", \
font_path=fpath, width=1024, height=1024, \
stopwords=set(stop_words)).generate(text)
plt.figure(figsize=(6,6))
plt.imshow(wordcloud)
plt.axis("off")
# plt.show()
plt.savefig(file_png)
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
file_png = sys.argv[1]
#
textin = '慶応 明治 大正 昭和 平成 令和 昭和 明治 昭和'
#
try:
create_wordcloud(textin,file_png)
except Exception as ee:
sys.stderr.write("*** error *** in create_wordcloud ***\n")
sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("*** 終了 ***\n")
#
# ------------------------------------------------------------------
実行方法
./wordcloud01.py out01.png