zoukankan      html  css  js  c++  java
  • 数据词云(wordcloud)

    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')




  • 相关阅读:
    Individual Reading Assignment
    Individual P1: Summary
    Individual P1: Preparation
    M1m2分析报告
    第二次阅读作业--12061161 赵梓皓
    代码互审报告
    结对编程————电梯整理报告
    读书问题之《编程之美》 -----12061161 赵梓皓
    SE Class's Individual Project--12061161 赵梓皓
    博客测试
  • 原文地址:https://www.cnblogs.com/xdlzs/p/11534392.html
Copyright © 2011-2022 走看看