"" "" "" "" "" "" "" "" "" "" "" "" "Masked Wordcloud ================ Using a mask From PIL import Image import numpy as NP import Matplotlib.pyplot as PLT from Wordcloud import Wordcloud, stopwords ' this I want to get rid of the word "reply" in the data, because it belongs to impurities, directly removed will be reported coding errors, with #-*-Coding:utf-8-*-can not be resolved, so find this method, change the default encoding, do not know what the meaning of the "" Import sys reload (sys
) sys.setdefaultencoding (' Utf-8 ') # Read the whole text. #text = Open (Path.join (d, ' alice.txt ')). Read () text=open (' Fzw.txt '). Read () # Read The Mask image # taken from # http://w Ww.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg alice_mask = Np.array (Image.open ("Alice_ Mask.png ")) #这里添加想要的形状, #stopwords = set (Stopwords) #stopwords. Add (" said ") ' background_color= ' white ' WC = Wordcloud (background_color= "White", max_words=2000, Mask=alice_mask, font_path= "Minisong.ttf", max_font_size=4 0,scale=0.8) #mask参数指定词云形状 # generate Word cloud # generate word Cloud mytext= ' ". Join (Jieba.cut (text)) Mytext=mytext.repl Ace (' reply', '] Wc.generate (mytext) # Store to File Wc.to_file ("Alice.png") # show Plt.imshow (WC, interpolation= ' bilinear ') plt.a
XIs ("Off") #plt. Figure () #plt. Imshow (Alice_mask, Cmap=plt.cm.gray, interpolation= ' bilinear ') #plt. Axis ("off") Plt.show () # The PiL way (if you don ' t have matplotlib) # image = Wordcloud.to_image () # image.show ()