from wordcloud import WordCloud # Read in the text file with open('book.txt') as f: text = f.read() # Generate the word cloud wordcloud = WordCloud().generate_from_text(text) # Show the word cloud import matplotlib.pyplot as plt plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show()
from wordcloud import WordCloud # Create a dictionary of word frequencies word_freq = {'hello': 3, 'world': 2, 'python': 5, 'word': 1} # Generate the word cloud wordcloud = WordCloud().generate_from_text(word_freq) # Show the word cloud import matplotlib.pyplot as plt plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show()In this example, we create a dictionary of word frequencies and pass it to the `generate_from_text` function. The resulting word cloud will display the words with their size proportional to their frequency in the dictionary. The package library used in both of these examples is the `wordcloud` package.