matplotlib-词云分析

在这里插入图片描述

#!/usr/bin/env python
# coding: utf-8

# #    第二课 Pandas文本数据分析
# ## 第七节 词云分析
# In[1]:
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# * 英文词云分析
# In[2]:
with open('./datasets/eng.txt', 'r') as f:
    eng_text = f.read()
# In[3]:
eng_text
# In[4]:
wordcloud = WordCloud().generate(eng_text)
#interpolation='bilinear'使文字更加圆润
plt.imshow(wordcloud, interpolation='bilinear')
#把坐标轴关掉
plt.axis('off')
plt.show()
# * 中文分词
# In[5]:
with open('./datasets/ch.txt', 'r', encoding='utf-8') as f:
    ch_text = f.read()
# In[6]:
ch_text
# In[7]:
import jieba
# In[8]:
words = jieba.cut(ch_text)
# In[9]:
words
# In[11]:
words_str = ' '.join(words)
# In[12]:
words_str
# In[13]:
#font_path字体路径
wordcloud = WordCloud(font_path='./fonts/simhei.ttf').generate(words_str)

plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()


# In[ ]:





猜你喜欢

转载自blog.csdn.net/lildn/article/details/114885740