Python | Word Cloud.

The word cloud is one of the interesting techniques for data visualization like infographic charts, graphs, etc. Word cloud could be the best option when analyzing customer’s and employee’s sentiments, Search engine optimization, pattern analysis of the large text, a quick overview of data, etc. In this tutorial, we are going to learn how to generate a word cloud from a text file using python.

Please make sure you have a python environment with worldcloud and matplotlib modules installed in it. If you have not installed these modules yet, you can install them by the following command.

WordCloud.

pip install worldcloud

Matplotlib

pip install matplotlib

Now we have all the required python modules to generate the word cloud. For the demo purpose, we are going to use the below sample text file. You can get it by clicking the download button or you can also use your own text file as well.

Source code:

import matplotlib.pyplot as plt
from wordcloud import WordCloud
text=open(‘sample.txt’).read()
wordcloud = WordCloud( background_color = ‘white’,
                            width = 1200,
                            height = 1000,
                            max_words=150,
                            scale=2,
                            ).generate(text)
plt.imshow(wordcloud)
plt.axis(‘off’)
plt.show()

Now save the file(tqb_wordcloud.py) and run it as shown in the snapshot below.

Output:

As shown in the above snapshot you can save the output in different formats (png, jpeg, jpg, pdf, etc.) by clicking the save button.