I have the following code:
'''code'''
import numpy as np
xvalues = np.array([47.6,49.6, 51.6, 53.6, 55.6, 57.6, 59.6, 61.6])
yvalues = np.array([397.944, 449.601, 498.021, 466.645, 531.621, 570.615, 594.162, 612.822 ])
yerrors = [75.413,75.413,75.413,75.413,75.413,75.413,75.413,75.413]
xerrors = [2,2,2,2,2,2,2,2]
import scipy.optimize as scpo
def Line(gradient, intercept, x):
return gradient*x + intercept
actual_fit_parameters, errors = scpo.curve_fit(Line, xvalues, yvalues)
fit_intercept = actual_fit_parameters[0]
fit_gradient = actual_fit_parameters[1]
ybestfit = Line(fit_gradient, fit_intercept, xvalues)
import matplotlib.pyplot as plt
plt.figure(1).add_axes((0,0,1,1))
plt.figure(1).add_axes((0, 0, 1, 1))
plt.xticks(np.arange(0.0, 62, 10))
plt.yticks(np.arange(-200, 900, 100))
difference = ybestfit - yvalues
normd = difference/yerrors
plt.plot(xvalues, normd, 'or')
plt.gca().set_ylim([-200,900])
plt.xlabel("2s + d")
plt.ylabel("Residuals")
plt.plot(np.arange(10.0,64.0,5.0), [0,0,0,0,0,0,0,0,0,0,0], '--', color = 'grey')
font = {'family': 'serif', 'weight': 'bold', 'size': 14}
'''code'''
and I'm trying to modify it, to give a similar graphical output to the code below, however, I can't seem to configure the axes right. Essentially, I want two graphs, combined, where one is a standard graph plot of the given values, with their errors, and then an attached residuals graph plot for the same data, with a normalised line of best of fit and also error bars, the above code achieves something very similar to this for a different set of data, however, I can't seem to adjust it correctly for mine. Hence, I need some help.
'''code'''
import matplotlib.pyplot as plt
from scipy import stats
import numpy as np
x = np.array([47.6,49.6, 51.6, 53.6, 55.6, 57.6, 59.6, 61.6])
y = np.array([397.944, 449.601, 498.021, 466.645, 531.621, 570.615, 594.162, 612.822 ])
yerr = [75.4134]*len(y)
xerr = [2]*len(x)
fig1 = plt.figure(1)
frame1=fig1.add_axes((.1,.3,.8,.6))
m, b = np.polyfit(x, y, 1)
print('gradient',m,'intercept',b)
plt.plot(x, m*x + b, '-', color='grey', alpha=0.5)
plt.plot(x,y,'.',color='black',markersize=6)
plt.errorbar(x,y,xerr=0,yerr=yerr,linestyle="None",color='black')
plt.ylabel('$1/\sqrt{F}$ $(N)$',fontsize=10)
plt.autoscale(enable=True, axis=u'both', tight=True)
plt.grid(False)
frame2=fig1.add_axes((.1,.1,.8,.2))
s = m*x+b #(np.sqrt(4*np.pi*8.85E-12)/2.23E-8)*x
difference = y-s
plt.plot(x, difference, 'ro')
frame2.set_ylabel('$Residual$',fontsize=10)
plt.xlabel('$2s+d_0$ $(m)$',fontsize=10)
'''code'''
can someone help correct the first set of code to achieve this?