温馨提示×

python词云图如何做

小亿
85
2024-02-18 15:21:20
栏目: 云计算

要制作Python词云图,可以使用第三方库WordCloud。以下是一个简单的示例代码,演示如何使用WordCloud库生成词云图:

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()

在这个示例中,我们首先读取一个文本文件,并使用WordCloud创建了一个词云对象。然后,我们将词云对象绘制在matplotlib图像中,并显示出来。您可以根据需要调整词云图的大小、背景颜色等参数。

0