2013-03-05 55 views
0

我有以下腳本讀取兩列ascii文件並生成1D圖。該圖有幾個峯值。我想要的是給所有的峯值,如第一個峯值1,第二個峯值2等等。峯值出現在X軸的等距位置上。有人能告訴我如何在Python中做到這一點。代碼 -給出高峯數字的陰謀

from pylab import* 


# Read the file. 
f2 = open('d012_SAXS-recomb.txt', 'r') 

# read the whole file into a single variable, which is a list of every row of the file. 
lines = f2.readlines()[2:-100] 

f2.close() 


# initialize some variable to be lists: 
x1 = [] 

y1 = [] 


# scan the rows of the file stored in lines, and put the values into some variables: 
for line in lines: 

    p = line.split() 

    x1.append(float(p[0])) 

    y1.append(float(p[1])) 


x = np.array(x1) 

y = np.array(y1) 


xlim(0.0,4.0) 


# now, plot the data: 
#subplot(211) 
plt.plot(x, y, color='orange',linewidth=2.0, linestyle='-', label='Arabic - LPP''\nRoman - SPP''\nAsterisk - CHOL') 
legend(loc='upper right') 

xlabel('q') 

ylabel('Intensity') 

plt.show() 
+0

您是否在討論如何計算正確的位置來使用pyplot.text()放置一些文本,如「peak1」,「peak2」等?語法是pyplot.text(x,y,string)。 – Stuart 2013-03-05 13:23:14

+1

您的問題更多地涉及如何找到高峯,還是更關聯如何放置文本? – Stuart 2013-03-05 13:28:35

+0

pyplot.text可以做到這一點,但在這種情況下,我必須手動給出每個峯的x,y座標,以便將數字1,2,3置於其上。有沒有什麼方法可以自動找到峯高並在峯頂放置1,2,3等數字? – user2095624 2013-03-05 14:24:52

回答

0

下面是一些找到第一個(最高)峯值的示例代碼。 (順便說一下,我在這裏使用pylab,所以plot和numpy模塊已經導入)。

x = linspace(0,10,501) 
y = exp(-0.2*x)*sin(x) 
k = y.argmax() 
plot(x,y) 
text(x[k],y[k],'Peak1') 

試着開始吧。

+0

感謝您的建議。這是一個好的開始。我會嘗試。 – user2095624 2013-03-06 08:50:59