0

I am trying to first fit and then plot a demand and supply curve in the same graph. I am using Scipy to help with the curve fit. However, from the documentation, I am not able to understand how I can add the supply curve given the code below?

from scipy.optimize import curve_fit 


def func(x, a, b, c):
    return a * np.exp(-b * x) + c

xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
np.random.seed(1729)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
plt.plot(xdata, ydata, 'b-', label='data')


popt1, pcov = curve_fit(func, xdata, ydata)
popt1

plt.plot(xdata, func(xdata, *popt), 'r-',
         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))



plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

My goal is to add the black curve which is the supply surve. How would I do this assuming an inverse function of the demand curve one using the code above? enter image description here

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Lroy_12374
  • 67
  • 6
  • Arguably, a Matplotlib question should be tagged as such, instead of `machine-learning`, `scipy`, `statistics`, or `scikit-learn` (edited). – desertnaut May 12 '20 at 10:30

1 Answers1

0

It is really easy to add the line, as you have asked. You are using matplotlib library which is very versatile. You can have a look here or here. Essentially you need to add one line of code towards the end of the fitting. Using your code only,

from scipy.optimize import curve_fit 


def func(x, a, b, c):
    return a * np.exp(-b * x) + c

xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
np.random.seed(1729)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
plt.plot(xdata, ydata, 'b-', label='data')


popt1, pcov = curve_fit(func, xdata, ydata)
popt1

plt.plot(xdata, func(xdata, *popt), 'r-',
         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))


#This is what you need to add
plt.plot(xdata,supplycurve)


plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
Shankar_Dutt
  • 115
  • 9