SoFunction
Updated on 2024-12-13

Example of code to generate English word cloud map based on python

 Using the wordcloud module, a cloud map is generated and the test text is:

Betty Botter bought some butter but she said the butter's bitter. If I put it in my batter it will make my batter bitter. So, she bought some better butter, better than the bitter butter and she put it in her batter and her batter was not bitter. So 'twas good that Betty Botter bought some better butter.

Generate cloud map code:

#Import Generate Cloud Map Module
from wordcloud import WordCloud
with open("F:/python primer/data2/fenci_test2.txt" ,encoding="utf-8")as file:
  #Read the text
  text=()
  #Set the font, background color, width, height and number of characters for the word cloud
  wordcloud=WordCloud(font_path="C:/Windows/Fonts/",
  background_color="black",width=600,
  height=300,max_words=50).generate(text)
  # Generate images
  image=wordcloud.to_image()
  #Display Pictures
  ()

Results:

This is the whole content of this article.