import pandas as pdimport numpy as npfrom pandas import Seriesfrom pandas import DataFramefrom wordcloud import WordCloud,ImageColorGeneratorimport matplotlib.pyplot as pltimport jiebafrom PIL import Image df =pd.read_csv(‘ZHILIAN_SPIDER.csv‘,header=None,index_col=False,names=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]) b=df[15]c=[ str(i).replace(‘元/月‘,‘‘).replace(‘以下‘,‘‘).replace(‘以上‘,‘‘).split(‘-‘) for i in b if i !=‘面议‘]d=[ float(ii) for i in c for ii in i if ii !=‘nan‘]avg=sum(d)/len(d) #wordcloud生成词云a=str(df[16]) ‘‘‘#普通版cut_text = " ".join(jieba.cut(a)) #分词wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",background_color="white",width=860, height=860, margin=2).generate(cut_text )plt.imshow(wordcloud,interpolation="bilinear")plt.axis("off")plt.show()wordcloud.to_file(‘test1.png‘)‘‘‘
#图像模仿版path_img = r"1.jpg"background_image = np.array(Image.open(path_img))cut_text = " ".join(jieba.cut(a)) #分词# 设置字体,不然会出现口字乱码,文字的路径是电脑的字体一般路径,可以换成别的# mask参数=图片背景,必须要写上,另外有mask参数再设定宽高是无效的wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",background_color="white",mask=background_image).generate(cut_text)# 生成颜色值image_colors = ImageColorGenerator(background_image)# 下面代码表示显示图片plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear")plt.axis("off")plt.show()wordcloud.to_file(‘test2.png‘)
原文地址:https://www.cnblogs.com/xdlzs/p/11534392.html
时间: 2024-09-28 08:23:26