Jan 3, 2025
wordcloud.wordcloud module:
pip install wordcloud
from wordcloud import WordCloud
import matplotlib.pyplot as plt
with statement to read the file (e.g., cl.txt).with open('cl.txt', 'r', encoding='utf-8') as file:
text = file.read()
WordCloud class with parameters:
width: Width of the word cloudheight: Height of the word cloudbackground_color: Set to 'white'wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
imshow method to display the word cloud:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
interpolation parameter improves visual quality.bilinear: Default, smoother outputnearest: Fastest, results in pixelationbicubic: Smoother but slowerfigure method to set figure size:
plt.figure(figsize=(10, 5))
plt.axis('off')