# -*- coding: utf-8 -*-""""Masked Wordcloud================Using a mask you can generate wordclouds in arbitrary shapes."""from os import pathfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltfrom wordcloud import WordCloud, STOPWORDSd = path.dirname(__file__)# Read the whole text.text = open(path.join(d, 'hua.txt')).read()# read the mask image# taken from# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpgalice_mask = np.array(Image.open(path.join(d, "bb.png")))wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask, stopwords=STOPWORDS.add("said"))# generate word cloudwc.generate(text)# store to filewc.to_file(path.join(d, "bb.png"))# showplt.imshow(wc)plt.axis("off")plt.figure()plt.imshow(alice_mask, cmap=plt.cm.gray)plt.axis("off")plt.show()
新闻热点
疑难解答