@@ -0,0 +1,1574 @@
+import numpy as np%0Aimport pandas as pd%0Afrom scipy import optimize%0Afrom matplotlib import pyplot as plt, cm, colors%0A%0Adef calc_R(x,y, xc, yc):%0A    %22%22%22 calculate the distance of each 2D points from the center (xc, yc) %22%22%22%0A    return np.sqrt((x-xc)**2 + (y-yc)**2)%0A%0Adef f(c, x, y):%0A    %22%22%22 calculate the algebraic distance between the data points and the mean circle centered at c=(xc, yc) %22%22%22%0A    Ri = calc_R(x, y, *c)%0A    return Ri - Ri.mean()%0A%0Adef leastsq_circle(x,y):%0A    # coordinates of the barycenter%0A    x_m = np.mean(x)%0A    y_m = np.mean(y)%0A    center_estimate = x_m, y_m%0A    center, ier = optimize.leastsq(f, center_estimate, args=(x,y))%0A    xc, yc = center%0A    Ri       = calc_R(x, y, *center)%0A    R        = Ri.mean()%0A    residu   = np.sum((Ri - R)**2)%0A    return xc, yc, R, residu%0A%0Adef plot_data_circle(x,y, xc, yc, R):%0A    f = plt.figure( facecolor='white')  #figsize=(7, 5.4), dpi=72,%0A    plt.axis('equal')%0A%0A    theta_fit = np.linspace(-np.pi, np.pi, 180)%0A%0A    x_fit = xc + R*np.cos(theta_fit)%0A    y_fit = yc + R*np.sin(theta_fit)%0A    plt.plot(x_fit, y_fit, 'b-' , label=%22fitted circle%22, lw=2)%0A    plt.plot(%5Bxc%5D, %5Byc%5D, 'bD', mec='y', mew=1)%0A    plt.xlabel('x')%0A    plt.ylabel('y')%0A    # plot data%0A    plt.plot(x, y, 'r-.', label='data', mew=1)%0A%0A    plt.legend(loc='best',labelspacing=0.1 )%0A    plt.grid()%0A    plt.title('Least Squares Circle')%0A    plt.show()%0A%0Afile_name = %22data/all.xlsx%22%0Aori_f = pd.read_excel(file_name, header=None)%0A%0A%0AE = ori_f%5B0%5D%0AN = ori_f%5B1%5D%0A%0Axc, yc, R, residu = leastsq_circle(E,N)%0A%0Aprint xc, yc%0A%0Aplot_data_circle(x=E, y=N, xc=xc, yc=yc, R=R)%0A

add circle fit demo code