NOTE: Before running this notebook, be sure to place your copy of RomeoAndJuliet.txt in the same folder as the notebook.
wordcloud module’s WordCloud class conda install -c conda-forge wordcloudfrom pathlib import Path
text = Path('RomeoAndJuliet.txt').read_text()
WordCloud fills non-white areas of a mask image with textimread function from the imageio module that comes with Anacondaimport imageio
mask_image = imageio.imread('mask_heart.png')
width and heightwidth and height, WordCloud uses the image’s sizeWordCloud assigns random colors from a color mapWordCloud’s keyword argumentsfrom wordcloud import WordCloud
wordcloud = WordCloud(width=1000, height=1000,
colormap='prism', mask=mask_image, background_color='white')
WordCloud’s generate method receives the text to use in the word cloud as an argument and creates the word cloud, which it returns as a WordCloud objectwordcloud = wordcloud.generate(text)
text argument, using the wordcloud module’s built-in stop-words listmax_words keyword argumentwordcloud = wordcloud.to_file('RomeoAndJulietHeart.png')
WordCloud’s fit_words method can create a word cloud from it, but does not remove the stop words from the dictionary%matplotlib inline
import matplotlib.pyplot as plt
plt.imshow(wordcloud)

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.