How can I create word cloud in Python?

Hi,

Please help me with the library and functions detail for creating word cloud in python. It will be great if you can help me with python codes to create it.

Thx,
Imran

@Imran,

There are multiple ways to create word cloud in Python. I will describe method using a library called work_cloud by Andreas Mueller.

Word_cloud library details:

The library can be downloaded from GitHub. You can find detailed instructions here:

Here is a blog post explaining the library:

Other requirements:

Word_cloud library requires Python’s imaging library PIL. So you will need to install it, if not done already.

Code for creating wordcloud:

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt

#Convert all the required text into a single string here 
#and store them in word_string

#you can specify fonts, stopwords, background color and other options

wordcloud = WordCloud(font_path='/Users/kunal/Library/Fonts/sans-serif.ttf',
                          stopwords=STOPWORDS,
                          background_color='white',
                          width=1200,
                          height=1000
                         ).generate(word_string)


plt.imshow(wordcloud)
plt.axis('off')
plt.show()

Hope this helps.

Kunal

Kunal,

I have created the TDM after cleaning up a data frame and removing stop words. Now how do I pass it on to create a word cloud.

Thanks,
Ratheen

@ratheen please refer this discussion