简体   繁体   English

Python在绘图中找到x值到相应的max y值

[英]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() 

Moving Average Plot 移动平均线图

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.

相关问题 如何在 plot 中给定指定的 Y 值找到对应的 X 值 - How to find corresponding X value given a specified Y value in a plot 如何在 plot 中找到给定 y 值的对应 x 值 - How to find the corresponding x value for a given y value in a plot 我应该如何找到对应于抛物线的最大或最小 y_value 的 x_value? (在 Python 中) - How should I find the x_value corresponding to the max or min y_value of a parabola? (in Python) 在X轴上查找对应于Y的任意值的值,该值不包含在用于在python中绘制数据的列表中 - To find the value on X axis corresponding to an arbitrary value of Y that doesn't contain in the list used to plot the data in python Python Matplotlib:在一个基本的plot上找到给定y值对应的x值 - Python Matplotlib: Find the corresponding x value of a given y value on a basic plot 找到与最大直方图对应的x值 - Find the x value corresponding to a histogram max 从 python plot (matplotlib) 中找到相应 x 的 y 值 - Find y value for respective x from python plot (matplotlib) 如何获得 seaborn 分布(和 plot 对应的行)的最大 x 值? - How to get the max x value of a seaborn distribution (and plot the corresponding line)? Python Seaborn Distplot Y值对应于给定的X值 - Python Seaborn Distplot Y value corresponding to a given X value 需要帮助理解一行代码如何使用最大 y 坐标在 python 中找到相应的 X 值 - Need help understanding how a line of code is using a maximum y coordinate to find the corresponding X value in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM