gpt4 book ai didi

python - 在给定焦点的情况下在 matplotlib 中绘制椭圆

转载 作者:行者123 更新时间:2023-11-30 22:42:05 30 4
gpt4 key购买 nike

椭圆的方程为:

sqrt((x-a1)**2 + (y-b1)**2) + np.sqrt((x-a2)**2 + (y-b2)**2) = c

焦点是(a1, b1)(a2, b2)c 也是已知的。如何使用 matplotlib 在 python 中绘制它?

感谢您的帮助。

最佳答案

您可以在某个变量t中以参数方式表示椭圆。您可以查看 Wikipedia例如,看看如何做到这一点。

在下面的代码中,我从您提供的参数中导出了参数形式所需的参数。

# Example focii and sum-distance
a1 = 1
b1 = 2
a2 = 5
b2 = 7
c = 9

# Compute ellipse parameters
a = c / 2 # Semimajor axis
x0 = (a1 + a2) / 2 # Center x-value
y0 = (b1 + b2) / 2 # Center y-value
f = np.sqrt((a1 - x0)**2 + (b1 - y0)**2) # Distance from center to focus
b = np.sqrt(a**2 - f**2) # Semiminor axis
phi = np.arctan2((b2 - b1), (a2 - a1)) # Angle betw major axis and x-axis

# Parametric plot in t
resolution = 1000
t = np.linspace(0, 2*np.pi, resolution)
x = x0 + a * np.cos(t) * np.cos(phi) - b * np.sin(t) * np.sin(phi)
y = y0 + a * np.cos(t) * np.sin(phi) + b * np.sin(t) * np.cos(phi)

# Plot ellipse
plt.plot(x, y)

# Show focii
plt.plot(a1, b1, 'bo')
plt.plot(a2, b2, 'bo')

plt.axis('equal')
plt.show()

这提供了您所需要的:

Ellipse plot

关于python - 在给定焦点的情况下在 matplotlib 中绘制椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42264232/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com