.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/masked.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_masked.py: Masked wordcloud ================ Using a mask you can generate wordclouds in arbitrary shapes. .. GENERATED FROM PYTHON SOURCE LINES 8-47 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_masked_001.png :alt: masked :srcset: /auto_examples/images/sphx_glr_masked_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_masked_002.png :alt: masked :srcset: /auto_examples/images/sphx_glr_masked_002.png :class: sphx-glr-multi-img .. code-block:: Python from os import path from PIL import Image import numpy as np import matplotlib.pyplot as plt import os from wordcloud import WordCloud, STOPWORDS # 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, 'alice.txt')).read() # read the mask image # taken from # http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg alice_mask = np.array(Image.open(path.join(d, "alice_mask.png"))) stopwords = set(STOPWORDS) stopwords.add("said") wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask, stopwords=stopwords, contour_width=3, contour_color='steelblue') # generate word cloud wc.generate(text) # store to file wc.to_file(path.join(d, "alice.png")) # show plt.imshow(wc, interpolation='bilinear') plt.axis("off") plt.figure() plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear') plt.axis("off") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.453 seconds) .. _sphx_glr_download_auto_examples_masked.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: masked.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: masked.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: masked.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_