【问题标题】:Python: how can I get the smoother-starting-point in graph?Python:如何在图中获得更平滑的起点?
【发布时间】:2018-10-12 00:24:36
【问题描述】:

我想要得到的是(x, y),对于给定的 x 和 y 值,y 值变得更平滑。

例如,

x = range(10)
y = [0.3, 0.37, 0.41, 0.52, 0.64, 0.68, 0.71, 0.72, 0.73, 0.74]
plt.plot(x, y)

我想得到图表开始稳定的红圈点(或近点)。

我该怎么做?

【问题讨论】:

  • 如何定义“流畅”或“稳定”?
  • @chrisz y 值停止变化很大的点!它不必是一个确切的点,但要接近一个。
  • 不是对立而是定义“很多”,25%...3%...

标签: python pandas numpy matplotlib


【解决方案1】:

您正在寻找的是 斜率 或更准确地说是一阶差分,要了解曲线开始平滑的位置,您可以计算一阶差分/斜率并找出第一个斜率低于某个阈值的索引:

import matplotlib.pyplot as plt
import numpy as np

x = np.array(range(10))
y = np.array([0.3, 0.37, 0.41, 0.52, 0.64, 0.68, 0.71, 0.72, 0.73, 0.74])

slopes = np.diff(y) / np.diff(x)
idx = np.argmax(slopes < 0.02)  # find out the first index where slope is below a threshold

fig, ax = plt.subplots()

ax.plot(x, y)
ax.scatter(x[idx], y[idx], s=200, facecolors='none', edgecolors='r')

【讨论】:

  • 很棒的解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 2014-07-14
  • 1970-01-01
  • 2018-07-28
  • 2019-06-21
相关资源
最近更新 更多