Python Create Word Cloud Image by Word Frequency or Weight Value – Python Wordcloud Tutorial

By | October 12, 2020

We have learned how to create a word cloud image by a text string in python. Here is the tutorial:

Python Creates Word Cloud Image: A Step Guide – Python Wordcloud Tutorial

However, if you only want to create a word cloud image using words and their frequency weight value, how to do?

Import wordcloud library

from wordcloud import WordCloud

wc = WordCloud(background_color='white', width = 300, height=300, margin=2)

Create word cloud image using word frequency

We set the word and its frequency first.

text = {'tutorialexample.com':5, 'python':3, 'tensorflow':2, 'numpy':3, 'deep learning':1}

Here text is a python dict, it contains each word and its frequency.

Then we can create a word cloud image using wc.fit_words() function.

wc.fit_words(text)
wc.to_file('wc.png')

The word cloud image is:

Python Create Word Cloud Image by Word Frequency

Create word cloud image using word and its weight value

Similar to create a word cloud image by word and its frequency, we can do like this:

text = {'tutorialexample.com':0.4, 'python':0.2, 'tensorflow':0.15, 'numpy':0.1, 'deep learning':0.15}
wc.fit_words(text)

wc.to_file('wc1.png')

The word cloud image is:

Python Create Word Cloud Image by Word Weight Value

Leave a Reply

Your email address will not be published. Required fields are marked *