2016-01-20 71 views
1

我有以下直方圖: enter image description hereMatplotlib直方圖 - 大於給定值時的曲線值

有人制作了上述代碼:

import matplotlib.pyplot as plt 
import numpy as num 


treshold_file='false_alarms.txt' 
with open(treshold_file, 'r') as f2: 
    lines = f2.readlines() 
data = [line.split() for line in lines] 
data1 = num.array(data) 
data2= data1.astype(float) 
plt.hist((data2), alpha=0.4,bins=[100,110,120,130, 140,150,160,180,200,250,300,350,400]) 
plt.xlabel("treshold") 
plt.ylabel("Frequency") 

我想繪製的每個箱數值大於或等於給定的閾值。

對於倉100,我想繪製樣品> 100的數量等

回答

2

我構建必要的數據後使用手動bar情節:

import numpy as np 
import matplotlib.pyplot as plt 

# dummy data 
data2 = np.random.randint(low=0,high=450,size=200) 

bins = [100,110,120,130,140,150,160,180,200,250,300,350,400] 
binwidths = np.diff(bincenters) 
binvals = [np.sum(data2>=thresh) for thresh in bins[:-1]] 

plt.figure() 
plt.bar(bins[:-1],binvals,width=binwidths,alpha=0.4) 
plt.xlabel('threshold') 
plt.ylabel('occurences') 

結果:

bar plot

陣列bins實際上是THRE列表(ndarray) sholds。對於每個閾值,我們計算出高於閾值的data2的值的數量,這些是條形圖的值,稱爲binvals。我們跳過最後的索引,在輸出中獲得正確的尺寸。