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.
Author | Blogger | Tech Lead | Elastic Stack | Innovator |View Profile
Leave a comment
Aug 4, 2018, 7:02:21 AM | Anurag Srivastava
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava
Aug 14, 2018, 4:47:56 PM | Anurag Srivastava
Dec 24, 2018, 5:25:28 PM | Anurag Srivastava
Jun 30, 2018, 12:07:47 PM | jitender yadav
Jun 8, 2018, 8:03:52 AM | Lovish Sharma
Feb 2, 2018, 7:29:35 PM | jitender yadav
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Jun 26, 2018, 4:50:18 PM | Anurag Srivastava
Jun 13, 2018, 3:44:01 PM | Anurag Srivastava
May 12, 2018, 4:47:07 PM | Anurag Srivastava
Jul 16, 2018, 5:00:02 PM | Anurag Srivastava
Apr 24, 2018, 2:52:28 PM | Anurag Srivastava
Jun 2, 2018, 10:49:54 AM | Anurag Srivastava
May 26, 2018, 6:42:02 PM | Anurag Srivastava
Jul 12, 2018, 7:40:51 PM | Anurag Srivastava
Jan 31, 2018, 6:11:29 AM | Anurag Srivastava