Generating Word Cloud in Python

Generating Word Cloud in Python

Generating a word cloud in Python is relatively straightforward using the wordcloud library. A word cloud (or tag cloud) visualizes word frequency from a given text, where the size of each word indicates its frequency or importance.

Here's how you can generate a word cloud:

1. Install the Required Libraries:

First, you need to install the necessary libraries. This usually involves the wordcloud, numpy, and matplotlib libraries.

pip install wordcloud numpy matplotlib 

2. Generate a Word Cloud:

You can then use the following code to generate a word cloud from a given text:

from wordcloud import WordCloud import matplotlib.pyplot as plt # Sample text text = """ Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. """ # Generate a word cloud wordcloud = WordCloud(width = 800, height = 800, background_color ='white', stopwords = set(['is', 'by', 'and', 'in', 'a', 'that', 'using']), min_font_size = 10).generate(text) # Plot the word cloud plt.figure(figsize = (8, 8), facecolor = None) plt.imshow(wordcloud) plt.axis("off") plt.tight_layout(pad = 0) plt.show() 

In the above code:

  • We set some basic configurations for the word cloud, like its width, height, and background color.
  • We've specified a set of stopwords to be ignored. Stopwords are words like "is", "and", etc., which might be frequent but are not informative.
  • min_font_size determines the smallest font size a word can have in the word cloud.

The word cloud will then be displayed using matplotlib.

You can further customize the appearance, add masks to make the word cloud a specific shape, and more by exploring the WordCloud class's options.


More Tags

operation collision-detection drop-down-menu line-intersection jasmine2.0 earthdistance django-permissions containers autostart rtp

More Programming Guides

Other Guides

More Programming Examples