Pythonđ â Making custom shaped Word Cloud from Wikipedia text
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.
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
Step 2:
Add the text that you want to use for your Word Cloud. I used the âIndiaâ Wikipedia page.
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.
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')