如何 return 对 (x,y) 坐标定义隐式曲线?

How to return pairs of (x,y) coordinates defining an implicit curve?

我已经通过我在此处找到的隐式函数脚本实现了这条心形曲线 (Wolfram Mathworld)

import matplotlib.pyplot as plt
import numpy as np

delta = 0.025
xrange = np.arange(-2, 2, delta)
yrange = np.arange(-2, 2, delta)
X, Y = np.meshgrid(xrange,yrange)

# F is one side of the equation, G is the other
F = X**2
G = 1- (Y - (np.abs(X))**(2/3))**2
plt.contour((F - G), [0])
plt.show()

如何提取定义此曲线的 x,y 对的结果数组?

可以说这有点“作弊”,但一个粗略、简洁的方法可以是在网格中获取大致满足条件的点并获取它们的坐标,如下所示:

# subset points that are very close to satifying the exact condition
# and get their indices
x_id, y_id = np.nonzero(abs(F-G) < 8e-3)
# get coordinates corresponding to the indices
plt.scatter(xrange[x_id], yrange[y_id])

xrange[x_id]yrange[y_id] 是所需的坐标数组。

我不是真正的 matplotlib 用户,可能有很多更好的方法来完成您的要求 - 所以请稍等,看看是否有人给出了正确的答案...

但是,目前,您似乎正在绘制数组 F-G,因此我使用 PIL 将其保存为 TIF(因为它可以存储浮点数),如下所示:

from PIL import Image

...
your code
...

contour = (F-G).astype(np.float32)
Image.fromarray(contour).save('contour.tif')

给出了这个:

检查contour发现范围是-116:

print(contour.min(), contour.max())
-1.0 15.869446307662544

所以,我推断你的分数可能接近于零:

points = (contour>-0.1) & (contour<0.1)

所以,我可以通过以下方式获得 X,Y 的列表:

Y, X = np.where(points)