# 一、实验目的
- 学习掌握Matplotlib库。
- 熟悉Artist模块。
- 掌握Pandas库的安装和其统计作图函数。
- 用词云图的展示方法展现实施乡村振兴战略的热点词语图像。
# 二、实验过程
- 访问Matplotlib官网,将demo上的文件复制到本地运行,体会matplotlib的使用。
- 总结Matplotlib绘图程序基本结构,以此为基础完成正余弦函数的图像绘制程序。
- 了解Artist模块的基本使用。
- 下载安装Pandas库
- 结合官网和教材熟悉Pandas库的基本操作。
- 了解Python第三方分词包(jieba)、词云包(WordCloud)的基本使用方法;
- 以《国务院关于实施乡村振兴战略的意见》为分析对象,基于Python环境搭建词云图开发环境,完成词云图分析
# 三、代码
import matplotlib.pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS
import jieba
import numpy as np
from PIL import Image
abel_mask=np.array(Image.open("E:/Python Cord/bg.png"))
text_from_file_with_apath=open('E:/Python Cord/xiangcun.txt').read()
wordlist_after_jieba=jieba.cut(text_from_file_with_apath,cut_all=True)
wl_space_split=" ".join(wordlist_after_jieba)
my_wordcloud=WordCloud(
width=600,
height=400,
background_color='white',
mask=abel_mask,
max_words=400,
stopwords=STOPWORDS,
font_path='E:/Python Cord/msyh.ttc',
max_font_size=100,
prefer_horizontal=0.8,
margin=2,
random_state=30,
scale=1.5
).generate(wl_space_split)
image_colors=ImageColorGenerator(abel_mask)
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()