I want to extract the points that matplotlib uses to generate the blue fit curve (I am not talking about data points I have generated). I am talking about points underlying matplotlib's curve.
# First generate some data
import numpy as np
# Seed the random number generator for reproducibility
np.random.seed(0)
x_data = np.linspace(-5, 5, num=50)
y_data = 2.9 * np.sin(1.5 * x_data) + np.random.normal(size=50)
# And plot it
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))
plt.scatter(x_data, y_data)
# Now fit a simple sine function to the data
from scipy import optimize
def test_func(x, a, b):
return a * np.sin(b * x)
params, params_covariance = optimize.curve_fit(test_func, x_data, y_data,
p0=[2, 2])
print(params)
# And plot the resulting curve on the data
plt.figure(figsize=(6, 4))
plt.scatter(x_data, y_data, label='Data')
plt.plot(x_data, test_func(x_data, params[0], params[1]),
label='Fitted function')
plt.legend(loc='best')
plt.show()
Expected result (circles: blue data points; curve: blue colored curve)
This series of points can be extracted (I just don't know how) and plotted to regenerate the fit curve/line (the blue curve) alone using another plotting program.