Wordclou: Generating word clouds from text
I. Word cloud settings
1Wc=wordcloud (width=400, height=200,#Canvas long, wide, default (400,200) pixels2Margin=1,#the distance between the word and the word3Background_color=' White',#Background Color4Min_font_size=3,max_font_size=none,#minimum, maximum font size displayed5MAX_WORDS=200,#maximum number of words to display6Ranks_only=none,#is it just the rankings7prefer_horizontal=.9,#the frequency at which the words are formatted horizontally is 0.9 (so the word vertically appears in the 0.1 frequency)8Mask=none,## Draw A word cloud with this parameter value, width and height are ignored when this parameter is not empty9Scale=1,#zoom in on the canvas by scaleTenColor_func=none,#A function that generates a new color, and if it is empty, uses the Self.color_func OneStopwords= (' Book'),#set the words that need to be masked, and if they are empty, use the built -in Stopwords ARandom_state=none,#returns a PIL color for each word -Font_step=1,#The font step, if the step is greater than 1, will speed up the operation but may result in a large error. -Mode="RGB",#when the parameter is "RGBA" and the Background_color is not empty, the background is transparent theRelative_scaling=.5,#the correlation between word frequency and font size -Regexp=none,#use regular expressions to separate input text -Collocations=true,#whether to include two words in a match -Colormap=none,#randomly assign colors to each word +Normalize_plurals=true,#whether to delete the trailing s of the word -Font_path="C:/windows/fonts/stfangso.ttf" #Font Path +)
wordcloud Word Cloud settings
Two. Generate Word Clouds
1 fit_words (frequencies) // generate Word Cloud 2 Generate (text)//based on the word frequency cloud 3 generate_from_frequencies (frequencies[, ...]) Generate Word Cloud 4 generate_from_text (text) // generate Word Cloud 5 process_text (text) according to the frequency of words // will be long text sub-word and remove the shielding words (here refers to English, Chinese participle or need to be implemented in other libraries, using the above fit_words (frequencies))6 recolor ([ Random_state, Color_func, ColorMap]) //Recolor existing output. Re-coloring is much faster than rebuilding the entire word cloud. 7 to_array () // convert to numpy array8 to_file (filename) //output to File
Related Methods
Three. Basic steps
1 ImportJieba#Jieba participle2 ImportMatplotlib.pyplot as Plt#Draw a graphic3 fromScipy.miscImportImread#working with Images4 fromWordcloudImportWordcloud, Stopwords, Imagecolorgenerator#Word Cloud5 6 #I. Text acquisition, using Jieba participle to get the words in the text7File=open ('Test.txt', encoding='UTF-8'). Read ()8word=' '. Join (Jieba.cut_for_search (file))9 Ten #two. Word Cloud background Image Acquisition OneImage=imread ('test.jpg')#parse the picture A - #three. Word Cloud settings -Wc=Wordcloud ( theMode='RGBA',#Set Transparent Background -Background_color=None, -Mask=image,#Word cloud shape set to background image -MAX_WORDS=100,#maximum number of words to display +Font_path="C:\\windows\\fonts\\stfangso.ttf",#set the font, otherwise Chinese may appear garbled -Scale=3#three times times the expansion + ) A at #Generate word Clouds -Image_colors = Imagecolorgenerator (image)#set font color based on background color -Wc.generate (Word)#generate word clouds based on text - - #Show -Plt.imshow (WC)#Display Word Cloud inPlt.axis ("off")#Close Axes -Plt.show ()#Display window toWc.to_file ('Test.png')#Save Picture
Word Cloud
Python3 Wordcloud Word Cloud