0

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.

user3398600
  • 685
  • 1
  • 5
  • 8
  • Unclear what you want. Currently there is no fitting done in your code. You are just plotting a function. Can you perhaps provide an example which is in line to your question about curve-fit and extracting data. Perhaps show an example with a plot and then tell us which points from the plot you want to extract – Sheldore Feb 05 '19 at 18:35
  • @Bazingaa, I have edited the post. Is it clear now? – user3398600 Feb 05 '19 at 19:30
  • So you want the data from the blue line? – Sheldore Feb 05 '19 at 19:35
  • The blue line data is nothing else but `y_blue_line = test_func(x_data, params[0], params[1])` and `x_blue_line = x_data`... What is the confusion here? – Sheldore Feb 05 '19 at 19:36
  • @Bazingaa, You are correct. In this example it works, Oversight. If you will, how will you do the same for this curve: http://seaborn.pydata.org/examples/distplot_options.html, the red curve on the upper right. I erred by not providing the example which is pertinent to my case. – user3398600 Feb 05 '19 at 20:09

1 Answers1

0

@Bazingaa provided an answer which helped. The question in my comment to him is answered by another post. That post is: Python Seaborn Distplot Y value corresponding to a given X value

user3398600
  • 685
  • 1
  • 5
  • 8