Python根据距离将坐标均匀分散在固定中心附近

问题描述 投票:1回答:2

我想根据每个数据点到中心的给定距离,在一个中心(2.5,2.5)周围散布许多数据点。我该怎么做,同时还能避免重复项/将它们均匀分散在中心?

提前感谢

python coordinates distance scatter-plot scatter
2个回答
2
投票
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(6, 6))

N = 120
angles = np.linspace(0, 2 * np.pi, N)
c_x, c_y = (2.5, 2.5)
x_s, y_s = [], []


distances = list(np.arange(0, 5.5, 0.5))

for distance in distances:
    for angle in angles:
        x_s.append(c_x + distance * np.cos(angle))
        y_s.append(c_y + distance * np.sin(angle))

    plt.scatter(x_s, y_s, c="b", s=4)
plt.show()

enter image description here


0
投票
import cmath
import numpy as np
from matplotlib import pyplot as plt
from itertools import starmap

c = np.array(list(starmap(cmath.rect, [(v//40+1, v*np.pi/20) for v in range(120)])))
x = c.real+2.5
y = c.imag+2.5
plt.scatter(x, y)

plot

© www.soinside.com 2019 - 2024. All rights reserved.