用matplotlib在已知顶点和两个“端点”的情况下绘制抛物线。

huangapple go评论59阅读模式
英文:

Graph parabola in matplotlib with vertex and 2 'end' points known

问题

尝试创建一个抛物线矢量,其中顶点和抛物线上的另外两个点已知。

示例...

  • 范围从0到10
  • 顶点 = [5,2]
  • 坐标1 = [1,1]
  • 坐标2 = [10,1]

用matplotlib在已知顶点和两个“端点”的情况下绘制抛物线。

任何帮助/建议都将不胜感激。

谢谢

英文:

Trying to create a parabolic vector of values where the Vertex and two other points along the parabola are known.

Example...

  • Range from 0 to 10
  • Vertex = [5,2]
  • Coordinate 1 = [1,1]
  • Coordinate 2= [10,1]

用matplotlib在已知顶点和两个“端点”的情况下绘制抛物线。

Any help/advice is greatly appreciated.

Thanks

答案1

得分: 1

import matplotlib.pyplot as plt
import numpy as np

# 点
x = [1, 5, 10]
y = [1, 2, 1]

# 使用 polyfit 来拟合通过这些点的二次多项式
poly_coeffs = np.polyfit(x, y, 2)

# 评估多项式找到的值
# 评估点的向量 xx
xmin = 0
xmax = 10
xx = np.linspace(xmin, xmax, 100)
yy = np.polyval(poly_coeffs, xx)  # y 坐标

# 画图
plt.figure()
plt.plot(x, y, 'or')
plt.plot(xx, yy)
plt.grid()
plt.ylim([-3, 4])
plt.xlim([-0.5, 12])

这会绘制出下面的图像:

用matplotlib在已知顶点和两个“端点”的情况下绘制抛物线。

英文:

I would use numpy to adjust a parabola passing by the points with polyfit, and then polyval to evaluate the polynomial found:

import matplotlib.pyplot as plt
import numpy as np

#points
x = [1, 5, 10]
y = [1, 2, 1]

poly_coeffs = np.polyfit(x,y,2) #fit a polynomial with degree=2

#evaluation points vector xx
xmin = 0
xmax = 10
xx = np.linspace(xmin, xmax, 100)
yy = np.polyval(poly_coeffs, xx) #y coords

#ploting
plt.figure()
plt.plot(x,y,'or')
plt.plot(xx,yy)
plt.grid()
plt.ylim([-3,4])
plt.xlim([-0.5,12])

This would draw the next image:

用matplotlib在已知顶点和两个“端点”的情况下绘制抛物线。

huangapple
  • 本文由 发表于 2023年6月30日 03:32:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定