这需要一些蛮力,但是下面的步骤会在正确的间隔内生成一个椭圆。这不是一个令人满意的解决方案,但它产生了我所追求的情节。在import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import math
# set of x,y values (with y errors) to which a linear fit will be applied
x = np.array([1, 2, 3, 4, 5])
y = np.array([1.7, 2.1, 3.5, 3.2, 4.4])
erry = np.array([0.2, 0.2, 0.2, 0.3, 0.3])
ax = plt.subplot(111)
# apply fit to x,y array weighted by 1/erry^2
p2, V = np.polyfit(x, y, 1, w=1/erry, cov=True)
# define a chi square function into which parameter estimates are passed
def chisq(param1, param0):
csq = np.sum(((param1*x + param0 - y)/erry) ** 2)
return csq
# arrange labels for the coefficients so matches form y = theta1*x + theta0
theta1 = p2[0]
theta0 = p2[1]
# show