Create a word cloud in Python

Anurag Srivastava
FAUN — Developer Community 🐾
2 min readOct 28, 2020

--

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 lets 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 for 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 the 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 the word cloud:

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

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 the word cloud library for more options. In case of any query please leave a comment.

Originally published at https://bqstack.com.

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

Author of “Mastering Kibana6.x”, “Kibana7 Quick Start Guide”, “Learning Kibana7”, &“Elasticsearch7 Quick Start Guide” books & AWS Certified Solutions Architect.