当前位置 : 主页 > 手机开发 > ROM >

数据词云(wordcloud)

来源:互联网 收集:自由互联 发布时间:2021-06-10
import pandas as pd import numpy as np from pandas import Series from pandas import DataFrame from wordcloud import WordCloud,ImageColorGenerator import matplotlib.pyplot as plt import jieba from PIL import Image df =pd.read_csv(‘ZHILIAN_
import pandas as pd
import numpy as np
from pandas import Series
from pandas import DataFrame
from wordcloud import WordCloud,ImageColorGenerator
import matplotlib.pyplot as plt
import jieba
from 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‘)

分享图片

 

 

分享图片

上一篇:Neo4j基础代码
下一篇:wxpy 微信机器人
网友评论