【问题标题】:Draw a smooth shape with coordinates on xy plane and implement the same in Python在 xy 平面上绘制带有坐标的平滑形状并在 Python 中实现相同
【发布时间】:2020-06-11 06:53:49
【问题描述】:

我在 xy 平面上有两组 4 个坐标,每组有 4 个半径。对于第一组,所有的半径都是相同的,形成一个完美的圆。至于第二组,4 个半径几乎没有什么不同,形成一个变形的圆。有人可以帮助我如何在图表上绘制这两个形状

Set1 : xy 坐标 : [(0,1),(1,0),(0,-1),(-1,0)], Radii[1,1,1,1]

Set2 : xy 坐标 : [(0,1),(1.2,0),(0,-0.9),(-1,0)], Radii[1,1.2,0.9,1]

我希望 set2 的形状看起来像一个变形的圆圈。所以,请帮我解决数学方程式和相同的python实现。

感谢您的宝贵时间。

【问题讨论】:

  • 也许这可以帮助你:stackoverflow.com/questions/47873759/…
  • 我不明白坐标代表什么。您是否只是在指定坐标处绘制具有给定半径的圆?如果没有,你能画出并附上一张你想要得到的图片吗?

标签: python math geometry polygon


【解决方案1】:

改编自How to fit a 2D ellipse to given points.

这应该是一个好的开始。

import numpy as np
import matplotlib.pyplot as plt

# circle = np.array([(0,1),(1,0),(0,-1),(-1,0)])
circle = np.array([(0,1),(1.2,0),(0,-0.9),(-1,0)]) 

# Extract x coords and y coords of the ellipse as column vectors
X = circle[:,0:1]
Y = circle[:,1:]

# Formulate and solve the least squares problem ||Ax - b ||^2
A = np.hstack([X**2, X * Y, Y**2, X, Y])
b = np.ones_like(X)
x = np.linalg.lstsq(A, b, rcond=-1)[0].squeeze()

# Print the equation of the ellipse in standard form
print('The ellipse is given by {0:.3}x^2 + {1:.3}xy +{2:.3}y^2 +{3:.3}x + {4:.3}y = 1'.format(*x))

# Plot the circle
plt.scatter(X, Y, label='Data Points')

# Plot the least squares ellipse
x_coord = np.linspace(-2,2,300)
y_coord = np.linspace(-2,2,300)
X_coord, Y_coord = np.meshgrid(x_coord, y_coord)
Z_coord = x[0] * X_coord ** 2 + x[1] * X_coord * Y_coord + x[2] * Y_coord**2 + x[3] * X_coord + x[4] * Y_coord
plt.contour(X_coord, Y_coord, Z_coord, levels=[1], colors=('r'), linewidths=2)

plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    相关资源
    最近更新 更多