Python中在圆形内生成均匀分布点的生成器

11
我被赋予生成一个不可见圆的同心圆上均匀(或多或少)分布点的任务。该函数应该接受半径列表和给定半径绘制点的数量作为参数。例如,对于半径为0,它应在(0,0)处绘制1个点。对于半径为1的圆,应沿着圆的周长绘制10个点,间隔为2pi / 10度。对于半径为2的圆,在周长上绘制20个点,间隔为2pi / 20度。
生成器应采用以下参数:
n,r_max,m
并应在半径为ri = i * r_max / n(其中i = 0,1,..,n)的坐标对上生成圆环。
每个圆环应该有n * i个在θ上均匀分布的点,其中 n_i = 1 for i=0; n_i = mi for i>0
当以以下方式调用函数时:
for r, t in genpolar.rtuniform(n=10, rmax=0.1, m=6):
      plot(r * cos(t), r * sin(t), 'bo')

它应该返回一个看起来像这样的图表: Plot

以下是我目前想到的内容:

def rtpairs(R, N):
        R=[0.0,0.1,0.2]
        N=[1,10,20]
        r=[]
        t=[]
        for i in N:
                theta=2*np.pi/i
            t.append(theta)

        for j in R:
            j=j
            r.append(j)

    plt.plot(r*np.cos(t),r*np.sin(t), 'bo')
    plt.show()

但我相当肯定有一种使用两个for循环更有效率的方法。

非常感谢。

5个回答

6
我想通了,代码如下:
import numpy as np
import matplotlib.pyplot as plt

T = [1, 10, 20, 30, 40, 50, 60]
R = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]



def rtpairs(r, n):

    for i in range(len(r)):
       for j in range(n[i]):    
        yield r[i], j*(2 * np.pi / n[i])

for r, t in rtpairs(R, T):
    plt.plot(r * np.cos(t), r * np.sin(t), 'bo')
plt.show()

3
这里有一种方法可以实现。
import numpy as np
import matplotlib.pyplot as plt

def circle_points(r, n):
    circles = []
    for r, n in zip(r, n):
        t = np.linspace(0, 2*np.pi, n, endpoint=False)
        x = r * np.cos(t)
        y = r * np.sin(t)
        circles.append(np.c_[x, y])
    return circles

当您将此函数传入适当的列表(一个包含每个圆的半径,另一个包含所需点数),它将返回一个坐标数组列表,每个圆对应一个。

r = [0, 0.1, 0.2]
n = [1, 10, 20]
circles = circle_points(r, n)

可以按照以下方式绘制。

fig, ax = plt.subplots()
for circle in circles:
    ax.scatter(circle[:, 0], circle[:, 1])
ax.set_aspect('equal')
plt.show()

enter image description here

这里我们看到更多圆的结果。

n = [1, 10, 20, 30, 40, 50, 60]
r = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
circles = circle_points(r, n)

fig, ax = plt.subplots()
for circle in circles:
    ax.scatter(circle[:, 0], circle[:, 1])
ax.set_aspect('equal')
plt.show()

enter image description here


可以用椭圆做同样的事情吗? - vicemagui
@VictorAguiar 你尝试过调整circle_points中定义x = r * np.cos(t)y = r * np.sin(t)的方程吗?如果你用椭圆的方程替换它们,它应该会画出同心椭圆。我需要检查一下,但它们可能存在间距问题。 - Steven C. Howell
1
你需要使用 np.linspace(0, two_pi, n, endpoint=False) 来使得圆周上有 n 个等间距的角度。否则,起始和结束角度会重合。这就是为什么图中显示了9个橙色点和19个绿色点的原因。 - JohanC

1
这是你可以实现这个的方式。
def circles(c_list: List[int]):
    g_d_list = []  # graph data list
    for g in c_list:
        # create length of circle list. In this instance
        # i'm multiplying by 8 each time but could be any number.
        lg = [g] * (8*g)
        ang = 360/len(lg)  # calculate the angle of each entry in circle list.
        ang_list = []
        for i in range(len(lg)+1):
            ang_list.append(ang*i)
        for i, c in enumerate(lg):
            # calculate the x and y axis points or each circle. in this instance
            # i'm expanding circles by multiples of ten but could be any number.
            x_axis = 0 + (10*g) * math.cos(math.radians(ang_list[i+1]))
            y_axis = 0 + (10*g) * math.sin(math.radians(ang_list[i+1]))
            # tuple structure ((axis tuple), circle size, circle colour)
            g_d_list.append(((x_axis, y_axis), 1, 'r'))

    fig, ax = plt.subplots()
    for c in range(len(g_d_list)):
        circle = plt.Circle(g_d_list[c][0], radius=g_d_list[c][1], fc=g_d_list[c][2])
        ax.add_patch(circle)
    plt.axis('scaled')
    plt.axis('off')  # optional if you don't want to show axis
    plt.show()

当提供一个包含所需圆数量的列表时,它会生成一个图表。例如,circles([1,2,3]) 返回。

enter image description here


0

你必须循环遍历所有半径的整个圆,因此你的绘图调用基本上被困在一些M*N的过程中。

代码细节可以稍微改进一下。首先,你的R列表已经包含了你想要的值;没有必要用相同的值构建一个新列表。你可以使用简单的列表推导式来构建t列表。

这是你想要的吗?

    N=[1,10,20]
    t = [2*np.pi/i for i in N]
    for R in N:
        plt.plot(R*np.cos(t),R*np.sin(t), 'bo')

    plt.show()

不,它会产生三个点环,其中第一个是退化的中心点。 - Prune
正如代码所写,它忽略了输入的 N 并将列表 R 乘以 np.sin()np.cos()。正如 @Lutzl 所提到的,当我运行此代码时,我也只看到了 3 个点。在上传您测试的代码时,是否有什么变化? - Steven C. Howell
“something” 包括在函数内部的简化测试用例。谢谢。 - Prune

0

我不懂Python但这个公式应该可以帮到你。

int ringNumber = 0

int n = ringNumber-1

((n/n+1)*60)-60 = 点之间的度数(零环除外,其点为中心)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接