What is the word cloud? Guide to creating a word cloud in Python

Contents

This article was published as part of the Data Science Blogathon.

Introduction

I've always been in love with data visualization since the day I started working on it. I always enjoy getting useful information from data. Before this, I only knew basic charts like bar charts, scatter diagrams, histogramas, etc. that are built into Tableau and Power BI in data visualization. By working every day on this task, I came across a lot of new graphics, as radial gauge charts, waffle graphics, etc.

Then, out of curiosity, I was recently looking for all types of charts that are used in data visualization, where this word cloud caught my attention and I found it very interesting. Up to now, seeing these word cloud images made me think that these are just random images where those words are randomly arranged, but i was wrong, and where it all started. After that, I tried creating a word cloud from small data in Tableau and Power BI. After that successful attempt, I wanted to test it by code making bar graphs, pie charts and other charts.

What is basically a word cloud?

Definition: A word cloud is a simple yet powerful visual representation object for word processing, showing the most frequent word in bigger and bolder letters, and with different colors. The smaller the word size, less will be the importance.

62760image1b-7335727
Sample Wor Cloud

Tag Cloud Uses

1) Top hashtags on social media (Instagram, Twitter): All over the world, social media is trending for the latest updates, so we can get the most used Hashtags that people use in their posts.

2) Hot topics in the media: When analyzing news articles, we can find the keywords in the headlines and extract the n most demanding topics and get the desired result, namely, the n most trending media topics.

3)Search term in an e-commerce: On an e-commerce shopping website, the owner can create the word cloud of the most searched shopping items. Therefore, you can get an idea of ​​which purchases are in high demand during a specific period.

Let's start coding in python to achieve this kind of word cloud

First, we need to install all libraries in jupyter notebook.

Then, and Python, there is a built-in wordcloud library that we will install. At the Anaconda command prompt, write the following code:

pip install wordcloud

If your anaconda environment supports conda, scribe:

conda install wordcloud

Even if, this can be achieved directly on the laptop itself, simply adding '!’ at the beginning of the code

Like:

!pip install wordcloud

Now, here I will generate the word cloud from the Wikipedia text of any topic. Therefore, I will need a Wikipedia library to access the Wikipedia API, what can be done by installing Wikipedia to anaconda command prompt as follows:

pip install wikipedia

Now there are some other libraries we need, they are numerous. matplotlib and pandas.

Hereinafter, we have all the libraries to create the tag cloud.

import wikipedia
result= wikipedia.page("MachineLearning")
final_result = result.content
print(final_result)
66158blogimage1-2743206

The result of the Machine Learning Wikipedia page

The above is the image of the result we got when retrieving the machine learning page from Wikipedia. There we can also see the scroll down, which means the whole page is retrieved.

Here, we can also get the page summary by summary method as shown below: Y

result= wikipedia.summary("MachineLearning", sentences=5)
print(result)

Here we have the sentence parameter, so we can use it to retrieve a specific number of lines.

90844blogimage2-5807874

The output of 5 prayers

Let's have the word cloud now

from wordcloud import WordCloud, StopWords
import matplotlib.pyplot as plt 
def plot_cloud(wordcloud):
    plt.figure(figsize=(10, 10))
    plt.imshow(wordcloud) 
    plt.axis("off");
wordcloud = WordCloud(width = 500, height = 500, background_color="pink", random_state=10).generate(final_result)
plot_cloud(wordcloud)

Empty words are words that have no meaning such as 'is', ‘son’, 'a', 'me’ and many more.

Wordcloud comes with a built-in library of stopwords, which will automatically remove stopwords from the text.

But, something interesting that comes here is that we can add our choice of stopwords in python using the stopwords.add function ().

The Wordcloud method will have width and height to set, I have set both as 500, the background color as pink. If you don't add a random state, every time you run your code, your word cloud will look different. Must be set as an int value.

Here is the desired word cloud, we will get from the previous code:

28980blogimage3-1096241

Al ver la figure anterior, we see that machine learning is the most used word, and there are some other words that are used frequently as a model, task, training, data. So we can conclude that machine learning is the task of training the data model.

We can also change the background color background color method and font colors by color map method here and we can also add the hash codes of the colors in the background color, but the mapcolor comes with the specific colors built in.

Let's change the background color to turquoise using its hash code and the font colors to blue:

from wordcloud import WordCloud, StopWords
import matplotlib.pyplot as plt
def plot_cloud(wordcloud):
    plt.figure(figsize=(10, 10))
    plt.imshow(wordcloud)
    plt.axis("off");
wordcloud = WordCloud(width = 500, height = 500, background_color="#40E0D0", colormap="ocean",  random_state=10).generate(final_result)
plot_cloud(wordcloud)
67701blog1-3901289

Here, I have specified ocean, if i add wrong colormap, jupyter will throw a value error and show me the available options for the colormap as shown below:

33699blog2-6317331

Wor cloud can also be deployed to any image using the PIL library.

Final notes

In this article, we argue about the word cloud, its definition, your application areas and your example in python using jupyter notebook.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker