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()
相关推荐
玉鸯6 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260856 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao7 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
人邮异步社区7 小时前
怎么把C语言学到精通?
c语言·开发语言
qetfw7 小时前
MXU:Tauri 2 + React 的 MaaFramework 跨平台 GUI 源码
前端·python·react.js·前端框架·开源项目·效率工具
心平气和量大福大8 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai8 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
用户8356290780518 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
এ慕ོ冬℘゜8 小时前
前端基础:什么是时间戳?JS获取时间戳三种方法与实战用途
开发语言·前端·javascript