python-2.7 - 如何在 matplotlib 中交互式查找和注释局部最大值(峰值)?

标签 python-2.7 matplotlib data-analysis

我正在尝试分析一些光谱以查找光谱峰,我编写了这个简单的代码,通过单击我想要查找的峰值之前和之后来查找两个 X 数据之间的最大 Y 值(峰值)。这有效,我可以获得峰值的坐标,但我想自动注释找到的峰值。

import matplotlib.pyplot as plt 
X=[1,2,3,4,5,6,7,8,9,10]
Y=[1,1,1,2,10,2,1,1,1,1]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(X,Y,label="prova") #plot the function
#plt.legend(loc=1, ncol=1, shadow=True)
plt.xlim(min(X) * 0.9, max(X) * 1.1)
plt.ylim(min(Y) * 0.9, max(Y) * 1.1)
plt.ylabel(r'Y axis')
plt.xlabel(r'X axis')
Diz=dict(zip(X,Y)) #create a dictionary that associate X with Y
Interval=[] #interval where the user search for peaks
PeaksList=[] #list of peaks found
def onclick(event):
    print 'First limit at =%f'%(event.xdata)
    Interval.append(event.xdata)
    if len(Interval)%2==0:
        a=Interval[-2]       
        b=Interval[-1]    
        if b<a: #if the user select first the highest value these statements filp it!
            A=b
            B=a
        else:
            A=a
            B=b
      #find the max Y value: the peak!
        peakY=0 #max Y value
        piccoX=0 #value of the X associate to the peak
        for i in [ j for j in X if A<j<B] :
            if Diz[i]>peakY:
                peakY=Diz[i]
                piccoX=i
        print "Interval: %f - %f  Peak at: %f " %(a,b,piccoX)
        PeaksList.append([piccoX,peakY])
        ax.annotate("picco", xy=(piccoX,peakY),  xycoords='data',
                xytext=(-50, 30), textcoords='offset points',
                arrowprops=dict(arrowstyle="->")
                )      


plt.show()         
cid = fig.canvas.mpl_connect('button_press_event', onclick)

这是我第二次点击后想要的: enter image description here

最佳答案

您必须重新绘制图形 - 只需将 plt.draw 添加到 onclick 方法即可。
此外,在显示图形之前,您必须连接事件。 当然,这里您的注释文本是“picco”而不是“5”。

尝试:

import matplotlib.pyplot as plt 
X=[1,2,3,4,5,6,7,8,9,10]
Y=[1,1,1,2,10,2,1,1,1,1]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(X,Y,label="prova") #plot the function
#plt.legend(loc=1, ncol=1, shadow=True)
plt.xlim(min(X) * 0.9, max(X) * 1.1)
plt.ylim(min(Y) * 0.9, max(Y) * 1.1)
plt.ylabel(r'Y axis')
plt.xlabel(r'X axis')
Diz=dict(zip(X,Y)) #create a dictionary that associate X with Y
Interval=[] #interval where the user search for peaks
PeaksList=[] #list of peaks found
def onclick(event):
    print 'First limit at =%f'%(event.xdata)
    Interval.append(event.xdata)
    if len(Interval)%2==0:
        a=Interval[-2]       
        b=Interval[-1]    
        if b<a: #if the user select first the highest value these statements filp it!
            A=b
            B=a
        else:
            A=a
            B=b
      #find the max Y value: the peak!
        peakY=0 #max Y value
        piccoX=0 #value of the X associate to the peak
        for i in [ j for j in X if A<j<B] :
            if Diz[i]>peakY:
                peakY=Diz[i]
                piccoX=i
        print "Interval: %f - %f  Peak at: %f " %(a,b,piccoX)
        PeaksList.append([piccoX,peakY])
        ax.annotate("picco", xy=(piccoX,peakY),  xycoords='data',
                xytext=(-50, 30), textcoords='offset points',
                arrowprops=dict(arrowstyle="->")
                )
        plt.draw()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()     

也许视觉上更有吸引力(使用spanselector):

import matplotlib.pyplot as plt 
from matplotlib.widgets import SpanSelector
X=[1,2,3,4,5,6,7,8,9,10]
Y=[1,1,1,2,10,2,1,1,1,1]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(X,Y,label="prova") #plot the function
#plt.legend(loc=1, ncol=1, shadow=True)
plt.xlim(min(X) * 0.9, max(X) * 1.1)
plt.ylim(min(Y) * 0.9, max(Y) * 1.1)
plt.ylabel(r'Y axis')
plt.xlabel(r'X axis')
Diz=dict(zip(X,Y)) #create a dictionary that associate X with Y
Interval=[] #interval where the user search for peaks
PeaksList=[] #list of peaks found
def onclick(xmin, xmax):
    print 'Interval: {0} - {1}'.format(xmin,xmax)
    peakY=0 #max Y value
    piccoX=0 #value of the X associate to the peak
    for i in [ j for j in X if xmin<j<xmax] :
        if Diz[i]>peakY:
            peakY=Diz[i]
            piccoX=i
    print "Peak at: %f " % piccoX
    PeaksList.append([piccoX,peakY])
    ax.annotate("picco", xy=(piccoX,peakY),  xycoords='data',
            xytext=(-50, 30), textcoords='offset points',
            arrowprops=dict(arrowstyle="->"))
    plt.draw()

span = SpanSelector(ax, onclick, 'horizontal', useblit=True,
                    rectprops=dict(alpha=0.5, facecolor='blue') )
plt.show()

关于python-2.7 - 如何在 matplotlib 中交互式查找和注释局部最大值(峰值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19398724/

相关文章:

Python BeautifulSoup 解析 HTML 时出错

尝试评估表达式时 Python eval() 语法错误

python - pandas:从列中的多重索引创建条形图

python - 将轴尺寸限制为另一个轴的尺寸

python-2.7 - 将列表作为 Python Pandas 和 Scikit-learn 中的一项功能的 Dictvectorizer

python - Pandas 随机读取一个NaN?

python - 拟合数据点,同时强制曲线形状

python - 在 python 中使用 pandas 将关键字与数据框列映射

sql - hive 中的复杂查询分组

python - 如何获取多列