Python 将峰值数指定给绘图

Python 将峰值数指定给绘图,python,matplotlib,Python,Matplotlib,我有下面的脚本,它读取两列的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

我有下面的脚本,它读取两列的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()

下面是一些查找第一个(最高)峰值的示例代码。(顺便说一句,我在这里使用的是pylab,因此plot和numpy模块已经导入)


试着开始吧。

您是说如何计算正确的位置,以便使用pyplot.text()放置一些文本,如“peak1”、“peak2”等?语法是pyplot.text(x,y,string)。您的问题更多的是关于如何找到峰值,还是与如何放置文本更相关?pyplot.text可以做到这一点,但在这种情况下,我必须手动为每个峰值提供x,y坐标,以便将数字1,2,3放在顶部。有没有什么方法可以让in自动找到峰值高度,并将数字(如1、2、3)放在峰值顶部?不知道数据的所有细节,我不知道找到您需要的所有峰值的最简单方法(或者“峰值”的标准是什么阈值等),但我可以为您提供第一个(或最高峰值)的简单答案。既然你说峰值在x轴上是等距的,那么通过相应地限制(切片)数据并搜索受限制集的峰值,其他峰值应该很容易找到。
x = linspace(0,10,501)
y = exp(-0.2*x)*sin(x)
k = y.argmax()
plot(x,y)
text(x[k],y[k],'Peak1')