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:
# 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.
You may like to read
Leave a comment
Sep 12, 2020, 3:58:55 PM | Anurag Srivastava
Aug 1, 2020, 6:19:45 PM | Anurag Srivastava
Mar 9, 2020, 8:45:41 AM | Rocky Paul
Dec 31, 2019, 1:33:53 PM | Anurag Srivastava
Dec 30, 2019, 11:20:08 AM | Anurag Srivastava
Dec 24, 2018, 5:25:28 PM | Anurag Srivastava
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
Jun 30, 2018, 12:07:47 PM | jitender yadav