Word Cloud in Python
In this article we will learn how we can implement word cloud in python using the wordcloud library.
Thinking about what is word cloud ?
The image that you are seeing above a word cloud .
WordCloud is the cluster of words of different sizes , colors and orientation in which the size of the word is determined by the frequency of times it occurs on a text.
To implement simple rectangular word cloud we need to install some python libraries -
- wordcloud
- matplotlib
- numpy
- PIL
Explanation of the libaries -
- wordcloud - This library provides the fuctionality through which wordcloud can be implemented without writing tons of codes.
- matplotlib - Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It is a plotting library which is used for data visualization.
- numpy - Numpy is a library for the , adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. In short we will be using this to give particular shape to our wordcloud image by doing its calculations.
- PIL - We had used pil library to import the image . Image should be of that shape which we want our wordcloud to be shaped .
Do i need to install these libaries to implement wordcloud ?
Yes, if you are working on an offline platform (like jupyter notebook) then we have to install it manually before importing those libraries.
No, if you are working on Google colab , then it will not be reqiured, because colab provides those fuctionality by default.
Installing those libraries -
- Installing python (Version greater than or equal to 2.7.9)
- Opening command prompt
- Write the commands below
pip install wordcloud
pip install matplotlib
pip install numpy
pip install pillow
( Note - If you are using jupyter notebook then use ! sign before the commands to install those libraries. Ex - ! pip install pillow )
Writing the program to generate simple rectangular word cloud -
Output -
Lets make our wordcloud bit more beautiful...
We can do it by -
- Removing the axis lines
- Providing it particular shape
- By changing the color of the background
Output -
Understanding the code -
shape = npy.array(Image.open("cloud.png"))
Here, the "cloud.png" is the shape which you want .You have to specify the path if it in the different directory.
cloud = WordCloud(background_color='white', mask = shape).generate(text)
Here we can provide properties like background color and mask which will provide shape to our cloud, generate() is the fuction provided by library so that our text can be put in the wordcloud module.
plt.axis('off')
This fuction is used to hide the axis or to turn off the axis.
Everytime when word cloud is executed words in the text will come with different colors and orienation.
Comments
Post a Comment