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()
相关推荐
沈浩(种子思维作者)几秒前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
saoys几秒前
Opencv 学习笔记:图像掩膜操作(精准提取指定区域像素)
笔记·opencv·学习
MM_MS11 分钟前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
꧁Q༒ོγ꧂38 分钟前
LaTeX 语法入门指南
开发语言·latex
njsgcs1 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_991 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
io_T_T1 小时前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
古城小栈1 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust
华研前沿标杆游学1 小时前
2026年走进洛阳格力工厂参观游学
python
Carl_奕然1 小时前
【数据挖掘】数据挖掘必会技能之:A/B测试
人工智能·python·数据挖掘·数据分析