Python finds x value for corresponding max y value in graph

I built a moving average from approximately 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 will be its frequency. I would like him to give me the coordinates on the plot, but if I get it, at least print it out, I will be satisfied. Sorry my programming skills as they are not the strongest. Here's a section of code I'm working on, and a link to the plot that it creates. I do not have enough points to publish 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() 

Moving Average Chart

+5
source share
1 answer

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 , 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

, API , . :

pylab.text(max_x, max_y, str((max_x, max_y)))
+5

All Articles