How to make a best-fit curve plot in Matplotlib?

I’m curious how one could solve the following physics problem with Python and Matplotlib.

In a class room a lighting meter was used to measure the illuminance E at the distance r. The data is as follows

E (lx), r (m)
663, 1
158, 2
71, 3
42, 4
19, 6
11, 8

Plot a curve that shows how the illuminance depends on the distance and the given points are made on bigger in the plot. My effort is as follows:

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 6, 8])
y = np.array([660, 160, 72, 42, 20, 10])


def fit_func(x, a, b, c):
    return a/(b*x**2 + c)


def connectpoints(x, y, p1, p2):
    x1, x2 = x[p1], x[p2]
    y1, y2 = y[p1], y[p2]
    plt.plot([x1, x2], [y1, y2], 'k-')


xmin = 1
xmax = 9
params = curve_fit(fit_func, x, y)

[a, b, c] = params[0]
y = []
numpoints = 100

for i in range(numpoints):
    y.append(a/(b*(i/numpoints*(xmax-xmin))**2+c))

x=[]
for i in range(numpoints):
    x.append(i/numpoints*(xmax-xmin))

plt.plot(x, y, 'ro')
for i in range(0,len(x)-1):
    if y[i] >0:
        connectpoints(x, y, i, i+1)

plt.xlim([xmin, xmax])
plt.ylim([0, 600])

plt.show()

But this shows hundred data points in the plot. Is it possible to make those extra points invisible such that the plot will contain only six data points and a best-fit curve between them?

But this shows hundred data points in the plot.

A quick look suggests you are overwriting your original xs and ys. Plot sampling data separately from your fitting line.

Example:

plt.plot(x_samp, y_samp, "ko", label="Data")
plt.plot(x_lin, y_model, "k--", label="Fit")

Where x_samp, y_samp is your original x, y arrays respectively. x_lin is an array of some range of values. y_model are the results based on the model associated with your fitted parameters. There are many examples on StackOverflow.