zoukankan      html  css  js  c++  java
  • 词云加显示条形图

    import pandas as pd #数据库操作
    import numpy as np
    from collections import Counter
    
    import matplotlib.pyplot as plt #绘图
    import jieba
    from scipy.misc import imread
    from wordcloud import WordCloud #词云可视化
    import matplotlib as mpl #配置字体
    from pyecharts import Geo #地理图
    mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
    mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
    # 配置绘图风格
    plt.rcParams["axes.labelsize"]=16 #axes是轴
    plt.rcParams["xtick.labelsize"]=14 #xtick是刻度
    plt.rcParams["ytick.labelsize"]=14
    plt.rcParams["legend.fontsize"]=12#legend输出字怎么设置字体大小
    plt.rcParams["figure.figsize"]=[15,15] #显示图像的最大范围
    plt.title("智联招聘python岗位学历要求")
    # plt.show()
    #数据预览
    df=pd.read_csv('all_results.csv',encoding='utf-8')
    # 使用head查看前几行数据(默认是前5行),不过你可以指定前几行
    # 使用tail查看后2行数据
    #print df.tail(2)
    #show_data=df.head(3)
    #barh 横着的条形图
    #下面进行计数统计,注意得到的是按照出现的频率降序排列
    #value_counts还是一个顶级的pandas方法。可用于任何是数组或者序列
    #rot 旋转刻度标签(0-360度)
    # df['qualifications'].value_counts().plot(kind='barh', rot=0)
    # plt.show()
    #
    # plt.title("智联招聘python岗位工作经验要求")
    # df['job_experience'].value_counts().plot(kind='bar', rot=0,color='r')
    # plt.show()
    stopwords = ['PYTHON','python','Python','工程师','(',')','/'] # 停止词
    finallist=[]
    seg_list=list(df["job_description"])
    info_attr = [x for x in jieba.cut(''.join(seg_list)) if len(x) >= 2 and x not in stopwords]  # 这里的x.word为词本身,x.flag为词性
    print(Counter(info_attr).most_common(100))
    
    
    
    # for seg in seg_list:
    #         if seg not in stopwords and len(seg.strip())>1:
    #             print(seg)
    #             finallist.append(seg)
    def get_topn_words(words, topn):
        c = Counter(words).most_common(topn)
        top_words_with_freq = {}
        with open('top{0}_words.txt'.format(topn), 'w+') as f:
            for x in c:
                f.write('{0},{1}
    '.format(x[0], x[1]))
                top_words_with_freq.setdefault(x[0], x[1])
        return top_words_with_freq
    def generate_word_cloud(img_bg_path, top_words_with_freq, font_path, to_save_img_path, background_color='white'):
        # 读取背景图形
        img_bg = imread(img_bg_path)
    
        # 创建词云对象
        wc = WordCloud(font_path=font_path,  # 设置字体
                       background_color=background_color,  # 词云图片的背景颜色,默认为白色
                       max_words=100,  # 最大显示词数为1000
                       mask=img_bg,  # 背景图片蒙版
                       max_font_size=50,  # 字体最大字号
                       random_state=30,  # 字体的最多模式
                       width=1000,  # 词云图片宽度
                       margin=5,  # 词与词之间的间距
                       height=700)  # 词云图片高度
    
        # 用top_words_with_freq生成词云内容
        wc.generate_from_frequencies(top_words_with_freq)
    
        # 用matplotlib绘出词云图片显示出来
        plt.imshow(wc)
        plt.axis('off')
        plt.show()
    
        # 如果背景图片颜色比较鲜明,可以用如下两行代码获取背景图片颜色函数,然后生成和背景图片颜色色调相似的词云
        # img_bg_colors = ImageColorGenerator(img_bg)
        # plt.imshow(wc.recolor(color_func = img_bg_colors))
    
        # 将词云图片保存成图片
        wc.to_file(to_save_img_path)
    generate_word_cloud('bg.jpg', get_topn_words(info_attr,500), 'yahei.ttf', 'santi_cloud.png')
    

      

  • 相关阅读:
    解决Ant design vue的Layout布局,左右布局侧边栏无效不显示问题
    寻找写代码感觉(十二)之 封装分页请求参数和返回参数
    解决关于interceptor拦截器跨域AccessControlAllowOrigin报错问题
    解决Vue3引入自定义组件报错Cannot find module ‘xxx‘ or its corresponding type declarations问题
    解决antD中关于table组件中报这样的警告warning.js?2149:7 Warning: [antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.]问题
    Centos 更改MySQL5.7数据库目录位置
    解决typescript:error TS2531: Object is possibly 'null'.问题
    解决npm WARN Local package.json exists, but node_modules missing, did you mean to install?问题
    弱监督学习文章略读记录
    弱监督学习最新文章略读
  • 原文地址:https://www.cnblogs.com/c-x-a/p/8519685.html
Copyright © 2011-2022 走看看