matplotlib-词云分析
#!/usr/bin/env python# coding: utf-8# #第二课 Pandas文本数据分析# ## 第七节 词云分析# In[1]:from wordcloud import WordCloudimport matplotlib.pyplot as plt# * 英文词云分析# In[2]:with open('./datasets/eng.txt', 'r') as f:en
·
#!/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[ ]:
更多推荐
已为社区贡献1条内容
所有评论(0)