How to Generate a Word Cloud of Any Shape in Python

6 Pages • 799 Words • PDF • 528.8 KB
Uploaded at 2021-09-24 14:43

This document was submitted by our user and they confirm that they have the consent to share it. Assuming that you are writer or own the copyright of this document, report to us by using this DMCA report button.


8/25/2019

How to Generate a Word Cloud of Any Shape in Python

How to Generate a Word Cloud of Any Shape in Python Julia Kho Sep 6, 2018 · 3 min read

. . . In this article, we explore how to generate a word cloud in python in any shape that you desire. We will go through an example of how to create a simple word cloud in the custom shape of a house as shown below.

https://blog.goodaudience.com/how-to-generate-a-word-cloud-of-any-shape-in-python-7bce27a55f6e

1/6

8/25/2019

How to Generate a Word Cloud of Any Shape in Python

If you are unfamiliar with a word cloud, it is an image of words where the size of each word indicates its frequency or importance. They are a powerful way to visualize text. Word clouds are easy to read and simple to understand. The key words stand out to the reader and are visually appealing amongst the audience. Creating a word cloud in a shape related to the theme of your project can make your visualization even more impressive.

. . .

PYTHON STEPS FOR GENERATING WORD CLOUD STEP 1: Import the following python libraries.

from wordcloud import WordCloud, STOPWORDS from PIL import Image import numpy as np import urllib import requests import matplotlib.pyplot as plt

NOTE 1: If you get an error that states “No module named ‘wordcloud’ ”, run the following command in your terminal to install wordcloud.

python -m pip install wordcloud

NOTE 2: If you get an error regarding PIL, navigate to the following file:

./anaconda3/lib/python3.6/site-packages/PIL/Image.py

Open the Image.py file and change the code below https://blog.goodaudience.com/how-to-generate-a-word-cloud-of-any-shape-in-python-7bce27a55f6e

2/6

8/25/2019

How to Generate a Word Cloud of Any Shape in Python

if PILLOW_VERSION != getattr(core, ‘PILLOW_VERSION’, None): raise ImportError(“The _imaging extension was built for another “ “version of Pillow or PIL:\n” “Core version: %s\n” “Pillow version: %s” % (getattr(core, ‘PILLOW_VERSION’, None), PILLOW_VERSION))

to the following

if core.PILLOW_VERSION != getattr(core, ‘PILLOW_VERSION’, None): raise ImportError(“The _imaging extension was built for another “ “version of Pillow or PIL:\n” “Core version: %s\n” “Pillow version: %s” % (getattr(core, ‘PILLOW_VERSION’, None), PILLOW_VERSION))

STEP 2: Add the text that you want to use for your word cloud. I used the following words related to homes.

words = 'access guest guest apartment area area bathroom bed bed bed bed bed bedroom block coffee coffee coffee coffee entrance entry francisco free garden guest home house kettle kettle kitchen kitchen kitchen kitchen kitchen kitchen living located microwave neighborhood new park parking place privacy private queen room san separate separate shared space space space street suite time welcome'

Step 3:

mask = np.array(Image.open(requests.get(‘http://www.clker.com/cliparts/O/i/ x/Y/q/P/yellow-house-hi.png', stream=True).raw))

To get the custom shape for the word cloud, look for an image that you want to use as a mask. In this example, I used the following image as an outline: http://www.clker.com/cliparts/O/i/x/Y/q/P/yellow-house-hi.png https://blog.goodaudience.com/how-to-generate-a-word-cloud-of-any-shape-in-python-7bce27a55f6e

3/6

8/25/2019

How to Generate a Word Cloud of Any Shape in Python

Step 4: This function takes in your text and your mask to generate a custom wordcloud. 1

def generate_wordcloud(words, mask):

2

word_cloud = WordCloud(width = 512, height = 512, background_color='white', stopwords=STOPWO

3

plt.figure(figsize=(10,8),facecolor = 'white', edgecolor='blue')

4

plt.imshow(word_cloud)

5

plt.axis('off')

6

plt.tight_layout(pad=0)

7

plt.show()

8

word_cloud_function.py hosted with ❤ by GitHub

view raw

Run the following line of code to call the generate_wordcloud function above.

https://blog.goodaudience.com/how-to-generate-a-word-cloud-of-any-shape-in-python-7bce27a55f6e

4/6

8/25/2019

How to Generate a Word Cloud of Any Shape in Python

generate_wordcloud(words, mask)

Your final code should look like this: 1

from wordcloud import WordCloud, STOPWORDS

2

from PIL import Image

3

import urllib

4

import requests

5

import numpy as np

6

import matplotlib.pyplot as plt

7 8

words = 'access guest guest apartment area area bathroom bed bed bed bed bed bedroom block coff

9

mask = np.array(Image.open(requests.get('http://www.clker.com/cliparts/O/i/x/Y/q/P/yellow-house

10 11

# This function takes in your text and your mask and generates a wordcloud.

12

def generate_wordcloud(words, mask):

13

word_cloud = WordCloud(width = 512, height = 512, background_color='white', stopwords=STOPW

14

plt.figure(figsize=(10,8),facecolor = 'white', edgecolor='blue')

15

plt.imshow(word_cloud)

16

plt.axis('off')

17

plt.tight_layout(pad=0)

18

plt.show()

19 20

#Run the following to generate your wordcloud

21

generate_wordcloud(words, mask)

gistfile1 p hosted ith ❤ b GitH b

ie

ra

Have fun making custom word clouds! You can also adjust the color and size to your liking. Feel free to share with me the word clouds you end up creating!

Sign Up to Get 100 FREE Raven Tokens! Raven is a decentralized and distributed deep-learning training protocol. Providing costefficient and faster training of deep neural networks.

Email https://blog.goodaudience.com/how-to-generate-a-word-cloud-of-any-shape-in-python-7bce27a55f6e

5/6

8/25/2019

How to Generate a Word Cloud of Any Shape in Python

Sign up I agree to leave Blog.goodaudience.com and submit this informa on, which will be collected and used according to Upscribe's privacy policy.



Formed on Upscribe

Connect with the Raven team on Telegram

Data Science

Machine Learning

AI

Arti cial Intelligence

Deep Learning

About

https://blog.goodaudience.com/how-to-generate-a-word-cloud-of-any-shape-in-python-7bce27a55f6e

Help

Legal

6/6
How to Generate a Word Cloud of Any Shape in Python

Related documents

6 Pages • 799 Words • PDF • 528.8 KB

228 Pages • 33,112 Words • PDF • 763.5 KB

15 Pages • 1,519 Words • PDF • 546 KB

46 Pages • 1,428 Words • PDF • 277.8 KB

4 Pages • 1,633 Words • PDF • 316 KB

9 Pages • 2,006 Words • PDF • 1 MB

1 Pages • 24 Words • PDF • 280 KB

9 Pages • 2,001 Words • PDF • 1.1 MB

322 Pages • 92,422 Words • PDF • 5.2 MB

4 Pages • 394 Words • PDF • 486.5 KB