Python🐍 — Making custom shaped Word Cloud from Wikipedia text

Geetika Mathur
3 min readAug 1, 2020

--

The easy way to generate a Word Cloud☁ is to type ‘word cloud’ on Google to locate one of those free websites available to generate Word Cloud. Quick and easy!đŸ’«. What’s more thrilling is to generate one yourself using python programming.

A Simple Word Cloud

What is Word Cloud?

💡Word Cloud or Tag Cloud is a data visualization technique used for representing text data in which the size of each word indicates its frequency or importance. They are an effective manner to visualise text. Word clouds are smooth to study and easy to understand. The key phrases stand out to the reader and are visually attractive among the audience.

In this article, we will learn:

  • How to create a basic word cloud from one to several text documents.
  • Adjust colour, size and number of text inside your word cloud.
  • Mask your word cloud into any shape of your choice.
  • Mask your word cloud into any colour pattern of your choice.

Python steps for generating a Word Cloud in Jupyter Notebook

Step 1:

Import the following python libraries.

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

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

!pip install wordcloud 
!pip install wikipedia
Python Libraries Installed

Step 2:

Add the text that you want to use for your Word Cloud. I used the ‘India’ Wikipedia page.

The text used for my Word Cloud

Step 3:

my_mask = np.array(Image.open(r'C:\Users\user\Desktop\maps.jpg'))

To get the custom shape for the word cloud, look for an image that you want to use as a mask.

I used this image

Step 4:

This function takes in your text and your mask to generate a custom word cloud.

def make_cloud(x):
cloud = WordCloud(background_color = 'black', max_words = 500, mask = my_mask, stopwords = STOPWORDS)
cloud.generate(x)
py.imshow(cloud)
py.axis("off")
py.savefig(r'C:\Users\user\Desktop\india_maps.jpg',dpi=500) #save img with name that u want
py.show()

Your Final Output

Image.open(r'C:\Users\user\Desktop\india_maps.jpg')
Your Final Word Cloud

--

--