☁️

Creating Word Cloud in Python

Jan 3, 2025

Creating a Word Cloud using Python

Overview

  • Making a word cloud using the Python library wordcloud.
  • Ensure a text file with 25-30 words is prepared in the same directory as your script.

Setup

Prerequisites

  • Install wordcloud module: pip install wordcloud
  • Import necessary libraries: from wordcloud import WordCloud import matplotlib.pyplot as plt

Reading the Text File

  • Use the with statement to read the file (e.g., cl.txt).
  • Specify encoding (UTF-8) for proper reading: with open('cl.txt', 'r', encoding='utf-8') as file: text = file.read()

Creating the Word Cloud

  • Create an instance of the WordCloud class with parameters:
    • width: Width of the word cloud
    • height: Height of the word cloud
    • background_color: Set to 'white'
    wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)

Displaying the Word Cloud

  • Use imshow method to display the word cloud: plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show()

Interpolation Parameter

  • The interpolation parameter improves visual quality.
  • Options include:
    • bilinear: Default, smoother output
    • nearest: Fastest, results in pixelation
    • bicubic: Smoother but slower

Adjusting Figure Size

  • Use figure method to set figure size: plt.figure(figsize=(10, 5))

Other Adjustments

  • Remove axes: plt.axis('off')
  • Change background color for different effects.

Conclusion

  • This process allows for easy visualization of word frequency from a text file.
  • Experiment with different parameters to customize the appearance of the word cloud.

Closing

  • Thank you for watching. See you in the next video!