python圆形生成器,在python中的圆圈中均匀分布的点的生成器

I am tasked with generating evenly (more or less) spaced points on concentric rings of an invisible circle. The function should take a list of radii, and number of points to plot for a given radius as arguments. For example for a radius of 0 it should plot 1 point at (0,0). For a circle of radius of 1, it should plot 10 points along the circumference of the circle, spaced out by an angle of 2pi/10. For a circle of radius 2, 20 points along the circumference, spaced out by an angle of 2pi/20.

The generator should take the following parameters:

n, r_max, m

and should generate rings of coordinate pairs at radii

r_i = i*r_max/n for i = 0,1,..,n.

Each ring should have n*i points uniformly distributed in θ where

n_i=1 for i=0; n_i = mi for i>0

When the function is called like this:

for r, t in genpolar.rtuniform(n=10, rmax=0.1, m=6):

plot(r * cos(t), r * sin(t), 'bo')

it should return a plot that looks like:

S8HoE.png

Here is what I've come up with so far:

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()

but I'm pretty sure there is a more efficient method using two for loops.

Many thanks

解决方案

I figured it out. The code goes like this:

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值