1#!/usr/local/bin/python3.8 2""" 3Minimal Example 4=============== 5 6Generating a square wordcloud from the US constitution using default arguments. 7""" 8 9import os 10 11from os import path 12from wordcloud import WordCloud 13 14# get data directory (using getcwd() is needed to support running example in generated IPython notebook) 15d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() 16 17# Read the whole text. 18text = open(path.join(d, 'constitution.txt')).read() 19 20# Generate a word cloud image 21wordcloud = WordCloud().generate(text) 22 23# Display the generated image: 24# the matplotlib way: 25import matplotlib.pyplot as plt 26plt.imshow(wordcloud, interpolation='bilinear') 27plt.axis("off") 28 29# lower max_font_size 30wordcloud = WordCloud(max_font_size=40).generate(text) 31plt.figure() 32plt.imshow(wordcloud, interpolation="bilinear") 33plt.axis("off") 34plt.show() 35 36# The pil way (if you don't have matplotlib) 37# image = wordcloud.to_image() 38# image.show() 39