How to find the data points from a graph

1.5k Views Asked by At

Please find the attached graph. I need to find the points "a" and "b". Kindly suggest any methods in python.

Graph is plotted by obtaining run times, which are observed as below: x = ([1000, 2000, 3000, 4000, 5000, 6000,7000, 8000, 9000]) and y = ([2314,802,519,417,358,318,302,284,280])

Need to find out "a" and "b" points so that i can use them individually for other tasks

Full code:

def piecewise_linear(x, x0, y0, k1, k2):
    return np.piecewise(x, [x < x0], [lambda x:k1*x + y0-k1*x0, lambda x:k2*x + y0-k2*x0])

perr_min = np.inf
p_best = None
for n in range(100):
    k = np.random.rand(10)*20
    p , e = optimize.curve_fit(piecewise_linear, x, y)
    perr = np.sum(np.abs(y-piecewise_linear(x, *p)))
    if(perr < perr_min):
        print "success"
        perr_min = perr
        p_best = p

xd = np.linspace(min(x), max(x), 100)
plt.figure()
plt.plot(x, y, "bo")
y_out = piecewise_linear(xd, *p_best)
plt.plot(xd, y_out)
plt.ylabel('Number of KeyFrames')
plt.xlabel('Threshold Values')
plt.show()

Graph

1

There are 1 best solutions below

10
On

I don't exactly understand your question. Do you want code to extract relevant data points from that graph image using some computer vision or do you just want the coordinates of the data points using the defined x and y lists? If latter is the case, you can do something like:

change_points = [] # to store the points you want
curr_slope = (y[1] - y[0]) / (x[1]-x[0]) # to be used for comparision
for i in range(2, len(y)):
    prev_slope = curr_slope
    curr_slope = (y[i]-y[i-1]) / (x[i]-x[i-1])
    if not (0.2 <= (curr_slope / prev_slope) <= 5):
        change_points.append((x[i-1], y[i-1]))

for point in change_points:
    print(point)

This prints (2000, 802). Is there anything that defines the green line? Otherwise, here I've set a ratio threshold to add only points which change the slope by a 'large enough' (in this case, by a factor of 5) amount.

Also, the parenthesis in your x and y initialisations are redundant. Just use square brackets.