user3595632
user3595632

Reputation: 5730

Python: how can I get the smoother-starting-point in graph?

What I want to get is the point, (x, y), where the y value getting smoother for given x and y values.

For example,

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)

enter image description here

I want to get the red-circle point (or near point) where the graph starts getting stable.

How can I do this?

enter image description here

Upvotes: 3

Views: 422

Answers (1)

akuiper
akuiper

Reputation: 215117

What you are looking for is slope or first order difference more exactly, to get an idea where the curve starts to smooth out, you can calculate the first order difference / slope and find out the first index where the slope is below a certain threshold:

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')

enter image description here

Upvotes: 5

Related Questions