[英]Python find x value to corresponding max y value in plot
I've plotted a moving average from about 300,000 data points and I need to find the maximum y-value for the peak in the signal and its corresponding x-value, which would be its frequency. 我已经绘制了大约300,000个数据点的移动平均值,我需要找到信号中峰值的最大y值及其相应的x值,即其频率。 I'd like for it to give me the coordinates on the plot itself but if I can get it to at least print them out I'd be satisfied.
我想它给我一些情节本身的坐标,但如果我能得到它至少打印出来我会满意的。 Excuse my programming skills as they are not the strongest.
请原谅我的编程技巧,因为他们不是最强的。 Here's the section of the code I'm working on and a link to the plot that it generates.
这是我正在处理的代码部分以及它生成的绘图的链接。 I don't have enough points to post the image.
我没有足够的积分来发布图片。
def movingaverage(interval, window_size):
window= np.ones(int(window_size))/float(window_size)
return np.convolve(interval, window, 'same')
x = freq[0:300000]
y = fft
pylab.plot(x,y,"k.")
y_av = movingaverage(y, 30)
pylab.plot(x, y_av,"r")
pylab.xlim(0,10)
pylab.ylim(0,1500)
pylab.xlabel("Frequency")
pylab.ylabel("Moving Average Magnitude")
pylab.grid(True)
pylab.show()
You should be able to do something like: 你应该可以这样做:
max_y = max(y_av) # Find the maximum y value
max_x = x[y_av.index(max_y)] # Find the x value corresponding to the maximum y value
print max_x, max_y
Edit 编辑
numpy arrays do not have an index method, so we should use argmax, as pointed out in the comments: numpy数组没有索引方法,所以我们应该使用argmax,如注释中所指出的:
max_y = max(y_av) # Find the maximum y value
max_x = x[y_av.argmax()] # Find the x value corresponding to the maximum y value
print max_x, max_y
I think this API should work to let you draw text onto your image. 我认为这个API应该可以让你在你的图像上绘制文字。 You could do that like so:
你可以这样做:
pylab.text(max_x, max_y, str((max_x, max_y)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.