Blog Detail

Covid-19 Global Tracker

preview image Programming
by Anurag Srivastava, Jun 30, 2018, 6:06:45 AM | 2 minutes |

Create word cloud in Python

In this blog we are going to learn word cloud creation in Python. There are some really good libraries available in Python using which we can create different types of word clouds.

We can use these words clouds to display them on websites or can print them to use on your desk. So sets start the word cloud creation by installing the wordcloud library. We can install it using pip:

sudo pip3 install wordcloud

Once wordcloud library is installed we can use it in our Python script for creating the word cloud. We also need matplotlib library fro creating the word plot. So please install matplotlib in case it is not installed.

Before creating the Python script we can create a text file to write all the words which we are going to display in word cloud. So create a file named const.txt and add each words line by line:


Now start creating the Python script by importing os, wordcount and matplotlib library:

from os import path
from wordcloud import WordCloud
import matplotlib.pyplot as plt

Now get the current directory and read the const.txt file:

d = path.dirname(__file__)
text = open(path.join(d, 'const.txt')).read()

Now create the word cloud:

wordcloud = WordCloud().generate(text)

Use matplotlib to display the generated image of word cloud:

plt.imshow(wordcloud, interpolation='bilinear')
# this will remove the axis display along with the image.

We can also pass additional parameters to WordCloud() method like max font size etc.

wordcloud = WordCloud(max_font_size=40).generate(text)

In this way we can create the word cloud. You can refer to word cloud library for more options. In case of any query please leave the comment.

Comments (0)

Leave a comment

Related Blogs

Elastic Stack Interview Questions

Sep 12, 2020, 3:58:55 PM | Anurag Srivastava

Introduction to Kibana

Aug 1, 2020, 6:19:45 PM | Anurag Srivastava

Create a Chess board in PHP

Mar 9, 2020, 8:45:41 AM | Rocky Paul

Handle Excel file using Python (Part 2)

Dec 31, 2019, 1:33:53 PM | Anurag Srivastava

Handle Excel file using Python (Part 1)

Dec 30, 2019, 11:20:08 AM | Anurag Srivastava

Create a Pie Chart in Kibana

Dec 24, 2018, 5:25:28 PM | Anurag Srivastava

Top Blogs

Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

Elasticsearch Rest API

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava