Minimal ExampleΒΆ

Generating a square wordcloud from the US constitution using default arguments.

  • simple
  • simple
import os

from os import path
from wordcloud import WordCloud

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()

# Generate a word cloud image
wordcloud = WordCloud().generate(text)

# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')

# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.imshow(wordcloud, interpolation="bilinear")

# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()

Total running time of the script: ( 0 minutes 1.001 seconds)

Gallery generated by Sphinx-Gallery