这就是我的结局。不是所有情况下都是完美的,它甚至不能顺利地解决这个示例问题,但我认为它已经足够满足我的需要了。谢谢丹,你的回答给了我正确的方向。在import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import cKDTree
def get_label_xy(tree, thresh, data, i):
neighbors = tree.query_ball_point([data[i, 0], data[i, 1]], thresh)
if len(neighbors) == 1:
xy = (-30, 30)
else:
mean = np.mean(data[:, :2][neighbors], axis=0)
if mean[0] == data[i, 0] and mean[1] == data[i, 1]:
if i < np.max(neighbors):
xy = (-30, 30)
else:
xy = (30, -30)
else:
angle = np.arctan2(data[i, 1] - mean[1], data[i, 0] - mean[0])
if angle > np.pi / 2:
xy = (-30, 30)
elif angle > 0:
xy = (30, 30)
elif angle > -np.pi / 2:
xy = (30, -30)
else:
xy