用python写一个周杰伦词云的可视化图表
时间: 2023-07-04 13:05:09 浏览: 48
好的,以下是用Python实现周杰伦歌词词云可视化的代码:
首先,需要安装 `jieba`、`wordcloud` 和 `matplotlib` 这三个库:
```python
!pip install jieba wordcloud matplotlib
```
然后,下载周杰伦的歌词文件(例如 `lyrics.txt`),并读入歌词:
```python
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读入歌词文件
with open('lyrics.txt', 'r', encoding='utf-8') as f:
lyrics = f.read()
```
接着,使用 `jieba` 对歌词进行分词,并统计每个词语的出现次数:
```python
# 对歌词进行分词
words = jieba.cut(lyrics)
# 统计词频
word_counts = {}
for word in words:
if len(word) > 1: # 只统计长度大于1的词语
word_counts[word] = word_counts.get(word, 0) + 1
```
最后,使用 `wordcloud` 生成词云,并使用 `matplotlib` 绘制图表:
```python
# 生成词云
wordcloud = WordCloud(font_path='msyh.ttc', background_color='white', width=800, height=600).generate_from_frequencies(word_counts)
# 绘制图表
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
```
其中,`font_path` 参数指定了字体文件的路径,`background_color` 参数指定了背景色为白色,`width` 和 `height` 参数指定了图表的大小。
完整的代码如下:
```python
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读入歌词文件
with open('lyrics.txt', 'r', encoding='utf-8') as f:
lyrics = f.read()
# 对歌词进行分词
words = jieba.cut(lyrics)
# 统计词频
word_counts = {}
for word in words:
if len(word) > 1: # 只统计长度大于1的词语
word_counts[word] = word_counts.get(word, 0) + 1
# 生成词云
wordcloud = WordCloud(font_path='msyh.ttc', background_color='white', width=800, height=600).generate_from_frequencies(word_counts)
# 绘制图表
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
```
执行上述代码,即可生成周杰伦歌词的词云可视化图表。