写一段代码,实现统计列表中词出现的次数并做成词云图
时间: 2024-02-11 08:09:28 浏览: 23
好的,我可以为您提供代码实现,需要使用`jieba`和`wordcloud`库。假设您已经安装了这两个库,下面是代码实现:
```python
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 列表数据
words_list = ['苹果', '梨', '香蕉', '苹果', '梨', '梨', '草莓', '香蕉', '香蕉', '梨']
# 统计词频
word_counts = {}
for word in words_list:
word_counts[word] = word_counts.get(word, 0) + 1
# 按词频排序
word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
# 生成词云图
wc = WordCloud(font_path='msyh.ttc', background_color='white', max_words=50)
wc.generate_from_frequencies(dict(word_counts))
# 绘制词云图
plt.figure(figsize=(8, 6))
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()
```
在上面的代码中,我们假设有一个列表`words_list`,我们使用一个字典统计每个词出现的次数,然后按词频排序,最后使用`WordCloud`生成词云图。您可以将代码中的`words_list`替换成您自己的列表数据,同时需要将`font_path`参数设置为您电脑上的中文字体文件路径。运行代码后,将会得到一个词云图展示每个词出现的频率。