You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python获取绘图中多个峰值的x、y坐标(含次峰值)

多谱带峰值检测实现方案

要定位谱图中的多个峰值(包括次峰),可以借助scipy.signal.find_peaks工具,它能灵活设置筛选条件识别所有有效峰值。以下是修改后的完整代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

# 加载数据
a2 = np.loadtxt('O-25.txt') 
x2 = a2[:, 0]
y2 = a2[:, 1]

# 归一化处理
y2Max = max(y2)
zz = y2 / y2Max

# 创建绘图对象
fig, ax = plt.subplots()
ax.plot(x2, zz, linewidth=2, label="")

# 检测峰值:设置最小高度和峰值间距
peaks, properties = find_peaks(zz, height=0.2, distance=50)

# 标注每个峰值
for peak_idx, peak_height in zip(peaks, properties['peak_heights']):
    x_val = x2[peak_idx]
    y_val = peak_height
    text = f"x={x_val:.3f}, y={y_val:.3f}"
    # 标注样式设置
    bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
    arrowprops = dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=60")
    ax.annotate(text, xy=(x_val, y_val), xytext=(0.94, 0.96 - 0.08*peaks.tolist().index(peak_idx)),
                xycoords='data', textcoords="axes fraction",
                arrowprops=arrowprops, bbox=bbox_props, ha="right", va="top")

# 设置图表标签和标题
plt.xlabel('波长(nm)', fontname='arial', fontsize=14)
plt.ylabel('能量(cm⁻¹)', fontname='arial', fontsize=14)
plt.title("O-")
plt.legend()
plt.show()

参数说明与调整

  • height=0.2:仅识别归一化后高度≥0.2的峰值,可根据谱图噪声水平调整,过滤无关小峰
  • distance=50:要求两个峰值在x轴上的索引间距至少为50,避免识别过于密集的相邻峰,可根据谱峰实际间距修改

运行代码后,所有符合条件的峰值都会被标注出坐标,包括次峰值。

内容的提问来源于stack exchange,提问作者AIme999

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:07:33