python 寻找数据拐点

python 复制代码
import numpy as np
import cv2
from scipy.signal import find_peaks

# 示例数据
y_data = [365.63258786, 318.34824281, 258.28434505, 228.8913738, 190.87220447, 158.28434505, 129.53035144, 111.95846645, 111.95846645, 120.26517572, 140.71246006, 161.79872204, 180.6485623,
          202.0543131, 241.03194888, 275.53674121, 313.87539936, 348.69968051, 391.19169329]
x_data = [186.26517572, 201.28115016, 220.76996805, 230.99361022, 242.81469649, 254.63578275, 263.90095847, 274.12460064, 274.12460064, 280.19488818, 284.02875399, 287.86261981, 291.05750799,
          295.84984026, 299.68370607, 303.8370607, 308.94888179, 313.10223642, 316.61661342]

x_data = np.array(x_data)
y_data = np.array(y_data)

# 使用 find_peaks 检测局部极大值(拐点)
peaks, _ = find_peaks(y_data, height=0)

# 使用 find_peaks 检测局部极小值(拐点)
# 通过检测 -y 的局部极大值来找到 y 的局部极小值
valleys, _ = find_peaks(-y_data)

# 创建空白图像
height, width = 600, 800
image = np.ones((height, width, 3), dtype=np.uint8) * 255

# 定义坐标轴范围和比例
x_min, x_max = min(x_data), max(x_data)
y_min, y_max = min(y_data), max(y_data)
x_scale = (width - 40) / (x_max - x_min)  # 留出20像素的边距
y_scale = (height - 40) / (y_max - y_min)

# 将数据点转换为图像坐标系
def to_image_coords(x, y):
    img_x = int((x - x_min) * x_scale + 20)
    img_y = height - int((y - y_min) * y_scale + 20)
    return img_x, img_y

# 绘制原始数据点和线条
for i in range(len(x_data) - 1):
    x1, y1 = to_image_coords(x_data[i], y_data[i])
    x2, y2 = to_image_coords(x_data[i + 1], y_data[i + 1])
    cv2.line(image, (x1, y1), (x2, y2), (0, 0, 0), 2)  # 黑色线条
    cv2.circle(image, (x1, y1), 3, (0, 0, 0), -1)  # 黑色点

# 绘制峰值点
for peak in peaks:
    x, y = to_image_coords(x_data[peak], y_data[peak])
    cv2.circle(image, (x, y), 5, (0, 0, 255), -1)  # 红色点

# 绘制谷值点
for valley in valleys:
    x, y = to_image_coords(x_data[valley], y_data[valley])
    cv2.circle(image, (x, y), 5, (255, 0, 0), -1)  # 蓝色点

# 绘制坐标轴
cv2.line(image, (20, height - 20), (width - 20, height - 20), (0, 0, 0), 2)  # x轴
cv2.line(image, (20, height - 20), (20, 20), (0, 0, 0), 2)  # y轴

# 添加标签
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image, 'X', (width - 30, height - 10), font, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, 'Y', (10, 30), font, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, 'Curve with Peaks and Valleys', (180, 40), font, 0.7, (0, 0, 0), 2, cv2.LINE_AA)

# 显示图像
cv2.imshow('Curve with Peaks and Valleys', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
相关推荐
ahccqw5 小时前
OpenCV 缩放 与 位移
人工智能·opencv·计算机视觉
小徐不徐说11 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang12 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
王中阳Go12 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
Wang's Blog12 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象32712 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
weixin_3077791313 小时前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展
Dxy123931021613 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷13 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
云和数据.ChenGuang13 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain