初次嘗試python的matplotlib,由於論文有一堆數據要處理,只能自學。就是所謂的用到什麼學什麼吧。html
下面記錄一下:首先統計全部閾值的頻率,用dictionary來存儲;參考官方網址對plt.bar()的各個參數解釋,創建相應的柱狀圖。python
代碼以下:api
# 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()
生成的圖片以下:
code
吐槽一下:插入圖片不能用pdf,致使放大失真嚴重htm