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

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

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

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # 点
  4. x = [1, 5, 10]
  5. y = [1, 2, 1]
  6. # 使用 polyfit 来拟合通过这些点的二次多项式
  7. poly_coeffs = np.polyfit(x, y, 2)
  8. # 评估多项式找到的值
  9. # 评估点的向量 xx
  10. xmin = 0
  11. xmax = 10
  12. xx = np.linspace(xmin, xmax, 100)
  13. yy = np.polyval(poly_coeffs, xx) # y 坐标
  14. # 画图
  15. plt.figure()
  16. plt.plot(x, y, 'or')
  17. plt.plot(xx, yy)
  18. plt.grid()
  19. plt.ylim([-3, 4])
  20. 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:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. #points
  4. x = [1, 5, 10]
  5. y = [1, 2, 1]
  6. poly_coeffs = np.polyfit(x,y,2) #fit a polynomial with degree=2
  7. #evaluation points vector xx
  8. xmin = 0
  9. xmax = 10
  10. xx = np.linspace(xmin, xmax, 100)
  11. yy = np.polyval(poly_coeffs, xx) #y coords
  12. #ploting
  13. plt.figure()
  14. plt.plot(x,y,'or')
  15. plt.plot(xx,yy)
  16. plt.grid()
  17. plt.ylim([-3,4])
  18. 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:

确定