Python Word Cloud Tutorial: Create in 5 Steps

To create a word cloud in Python, you can use the third-party library WordCloud. Here is a simple example code demonstrating how to generate a word cloud using the WordCloud library.

from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 读取文本文件
text = open('text.txt').read()

# 创建一个WordCloud对象
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)

# 绘制词云图
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')

# 显示词云图
plt.show()

In this example, we start by reading a text file and creating a word cloud object using WordCloud. We then plot the word cloud object on a Matplotlib image and display it. You can adjust parameters such as the size and background color of the word cloud image as needed.

bannerAds