2013-03-05 78 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