|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #coding:utf-8
- import pandas as pd
- import jieba
- import re
- import imageio
- from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
- import matplotlib.pyplot as plt
- data = pd.read_excel('D:/sunxiaoyan/7154308325.xls') # 读取Excel转为dabaframe
- df = pd.DataFrame(data)
- nrows = df.shape[0] # 获得一共有多少行
- file1 = df.正文.dropna(how='any') # 去掉空值
- file2 = df.正文.dropna(how='any')
- text1 = ''.join(i for i in file1) # 把所有字符串连接成一个长文本
- text2 = ''.join(i for i in file2)
- responsibility = re.sub(re.compile(',|;|\.|、|。'), '', text1) # 去掉逗号等符号
- requirement = re.sub(re.compile(',|;|\.|、|。'), '', text2)
- wordlist1 = " ".join(jieba.cut(responsibility, cut_all=True)) #
- # wordlist1=" ".join(jieba.cut(requirement,cut_all=True))#
- font_path = r'C:\Windows\Fonts\simkai.ttf'
- stopwords = list(STOPWORDS) + ['原图', '视频', '电视']
- # stopwords = list(STOPWORDS)+['以上学历','优先','计算','经验','学历','上学','熟练','使用','以上']
- bgimg = imageio.imread(r'D:/sunxiaoyan/sxy.jpg') # 设置背景图片
- wc = WordCloud(font_path=font_path, # 设置字体
- background_color="white", # 背景颜色
- max_words=500, # 词云显示的最大词数
- stopwords=stopwords, # 设置停用词
- mask=bgimg, # 设置背景图片
- max_font_size=100, # 字体最大值
- random_state=42, # 设置有多少种随机生成状态,即有多少种配色
- width=1000, height=860, margin=2, # 设置图片默认的大小,margin为词语边缘距离
- ).generate(wordlist1)
- image_colors = ImageColorGenerator(bgimg) # 根据图片生成词云颜色
- plt.imshow(wc)
- plt.axis("off")
- plt.savefig("sxy.jpg") # 必须在plt.show之前,不是图片空白
- plt.show()
复制代码 |
|