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 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:
data-science
php
python
pandas
machine-learning
elastic
kibana
mySQL
postgreSQL
noSQL
linux
integration
microservices
git
optimization
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.
Comments (0)
Leave a comment
Related Blogs
Introduction to Kibana
Aug 1, 2020, 6:19:45 PM | Anurag Srivastava
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava
Introduction to Elasticsearch Aggregations
Aug 14, 2018, 4:47:56 PM | Anurag Srivastava
Wildcard and Boolean Search in Elasticsearch
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Basics of Data Search in Elasticsearch
Aug 4, 2018, 7:02:21 AM | Anurag Srivastava
Top Blogs
Wildcard and Boolean Search in Elasticsearch
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Elasticsearch REST APIs
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
How to count number of words in a HTML string and find Read time in Python 3
Jun 30, 2018, 12:07:47 PM | jitender yadav
Create a Chess board in PHP
Mar 9, 2020, 8:45:41 AM | Rocky Paul
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava