7/14 の実験ゼミで用いたヒストグラムを書くコード

hist.py
with open('wave0.txt') as f:
      lines = f.readlines()
 
#必要なモジュールをインポートする
import matplotlib.pyplot as plt
import numpy as np
 
#色の指定
c1,c2 ='blue','green'
 
#グラフを表示する領域をfigオブジェクトとして作成
fig = plt.figure(figsize =(10,10), facecolor='lightblue')
 
#グラフを描画するsubplot領域を作成-> 引数は行、列、場所-> この場合、2*1に分割して何番目か
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
 
#グラフをプロットする
ax1.plot([int (line) for line in lines[:1023]],color=c1,label="wave")
ax1.set_xlim([500,530]) #プロットを500から530の範囲でする
 
N_POINTS = 1023 # sample数
data = np.array([int (line) for line in lines]) #テキストデータを整数にする-> arrayに詰め込む
length = len(data)//N_POINTS  #lengthで全イベント数を出す-> 割り算 //で商のみを出す 
data_res =data [:length*N_POINTS].reshape(-1,N_POINTS) #reshapeで配列に詰める
 
#ヒストグラムを書く
def intg(d):
  return (d[510:530] -d[:500].mean()).sum()  #510~530で積分する
ax2.hist([-intg(d) for d in data_res],bins=128,color=c2,label="hist")  #ビン数=128
 
#タイトル
ax1.set_title('wave')
ax2.set_title('hist')
 
#軸ラベルの指定
ax1.set_xlabel("Nsample")
ax1.set_ylabel("value")
ax2.set_xlabel("ADC_value")
ax2.set_ylabel("event_number")
 
#凡例表示
ax1.legend(loc ='upper right')
ax2.legend(loc ='upper right')
 
plt.show()