zoukankan      html  css  js  c++  java
  • matplotlib的初次使用

    初次尝试python的matplotlib,因为论文有一堆数据要处理,只能自学。就是所谓的用到什么学什么吧。

    下面记录一下:首先统计所有阈值的频率,用dictionary来存储;参考官方网址对plt.bar()的各个参数解释,建立相应的柱状图。

    代码如下:

    # encoding:utf-8
    import numpy as np
    import math
    from matplotlib import pyplot as plt
    
    # 0的字典
    dic0 = {}
    # 1的字典
    dic1 = {}
    
    def autolabel(rects):
        for rect in rects:
            height = rect.get_height()
            plt.text(rect.get_x() + rect.get_width() / 2. - 0.2, 1.03 * height, '%d' % height)
    
    total_width,n = 0.4,2
    width = total_width/n
    
    with open(r'/Users/binryang/Desktop/运行结果/balloons_qp25_16.txt', 'r') as f:
        for line in f:
            a = line.split()
            key = int(math.ceil(float(a[1])))
            if key == 0 or key > 10:
                continue
            if a[0] == '0':
                if key in dic0.keys():
                    dic0[key] = dic0[key] + 1
                else:
                    dic0[key] = 1
            else:
                if key+width in dic1.keys():
                    dic1[key+width] = dic1[key+width] + 1
                else:
                    dic1[key+width] = 1
    
    
    # items0 = dic0.items()
    # items0.sort()
    #
    # items1 = dic1.items()
    # items1.sort()
    #
    # print dic0
    # print dic1
    
    # plt.figure()
    # plt.plot(dic0.keys(),dic0.values())
    # plt.plot(dic1.keys(),dic1.values())
    
    m = plt.bar(dic0.keys(), dic0.values(),width,label='no-division')
    n = plt.bar(dic1.keys(),dic1.values(),width,label='division')
    autolabel(m)
    autolabel(n)
    plt.xlabel('difference value')
    plt.ylabel('numbers')
    plt.title('numbers of difference value')
    plt.legend()
    
    # plt.xticks(dic0.keys())
    
    # plt.subplot(212)
    # plt.bar(dic1.keys(),dic1.values())
    # plt.xlabel('difference value')
    # plt.ylabel('numbers')
    # plt.xticks(dic1.keys())
    plt.savefig('bar.pdf')
    plt.show()
    
    
    

    生成的图片如下:

    吐槽一下:插入图片不能用pdf,导致放大失真严重

  • 相关阅读:
    熟悉常用的HDFS操作
    爬虫爬取小说网站
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习:词频统计
    最近在学习多元分析,有空放上来分享
    机器学习基石作业一15-20题(Python实现)
    2018十月份
  • 原文地址:https://www.cnblogs.com/bingo2-here/p/8988098.html
Copyright © 2011-2022 走看看