繁体   English   中英

如何在python中使用散点图创建一个在其周边均匀分布点的圆

[英]How to create a circle with uniformly distributed dots in the perimeter of it with scatterplot in python

假设我有一个圆x**2 + y**2 = 20 现在我想在散点图n_dots圆周边的n_dots点绘制圆圈。 所以我创建了如下代码:

n_dots = 200
x1 = np.random.uniform(-20, 20, n_dots//2)
y1_1 = (400 - x1**2)**.5
y1_2 = -(400 - x1**2)**.5
plt.figure(figsize=(8, 8))
plt.scatter(x1, y1_1, c = 'blue')
plt.scatter(x1, y1_2, c = 'blue')
plt.show()

但这表明圆点不均匀分布在圆圈中的所有位置。 输出是:

输出散点图

那么如何在散点图中创建一个带圆点的圆,其中所有的点均匀分布在圆的周边?

沿圆周边绘制均匀间隔点的简单方法是将整个圆划分为相同的小角度,从圆中心到所有点的角度都可以得到。 然后,可以计算每个点的坐标(x,y)。 以下是执行任务的代码:

import matplotlib.pyplot as plt
import numpy as np

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

n_dots = 120   # set number of dots
angs = np.linspace(0, 2*np.pi, n_dots)  # angles to the dots
cx, cy = (50, 20)  # center of circle
xs, ys = [], []    # for coordinates of points to plot
ra = 20.0          # radius of circle

for ang in angs:
    # compute (x,y) for each point
    x = cx + ra*np.cos(ang)
    y = cy + ra*np.sin(ang)
    xs.append(x)   # collect x
    ys.append(y)   # collect y

plt.scatter(xs, ys, c = 'red', s=5)  # plot points 
plt.show()

由此产生的情节: 在此输入图像描述

或者,可以使用numpy的广播性质并缩短代码:

import matplotlib.pyplot as plt
import numpy as np

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

n_dots = 120   # set number of dots
angs = np.linspace(0, 2*np.pi, n_dots)  # angles to the dots
cx, cy = (50, 20)  # center of circle
ra = 20.0          # radius of circle

# with numpy's broadcasting feature...
# no need to do loop computation as in above version
xs = cx + ra*np.cos(angs)
ys = cy + ra*np.sin(angs)

plt.scatter(xs, ys, c = 'red', s=5)  # plot points 
plt.show()

对于也适用于2D的非常通用的答案:

import numpy as np
import matplotlib.pyplot as plt


def u_sphere_pts(dim, N):
    """
    uniform  distribution points on hypersphere
    from uniform distribution in n-D (<-1, +1>) hypercube,
    clipped by unit 2 norm to get the points inside the insphere,
    normalize selected points to lie on surface of unit radius hypersphere
    """
    # uniform points in hypercube
    u_pts = np.random.uniform(low=-1.0, high=1.0, size=(dim, N))

    # n dimensional 2 norm squared
    norm2sq = (u_pts**2).sum(axis=0)

    # mask of points where 2 norm squared  < 1.0
    in_mask = np.less(norm2sq, np.ones(N))

    # use mask to select points, norms inside unit hypersphere
    in_pts = np.compress(in_mask, u_pts, axis=1)
    in_norm2 = np.sqrt(np.compress(in_mask, norm2sq))  # only sqrt selected

    # return normalized points, equivalently, projected to hypersphere surface
    return in_pts/in_norm2


# show some 2D "sphere" points
N = 1000
dim = 2
fig2, ax2 = plt.subplots()
ax2.scatter(*u_sphere_pts(dim, N))
ax2.set_aspect('equal')
plt.show()

在此输入图像描述

# plot histogram of angles

pts = u_sphere_pts(dim, 1000000)
theta = np.arctan2(pts[0,:], pts[1,:])
num_bins = 360
fig1, ax1 = plt.subplots()
n, bins, patches = plt.hist(theta, num_bins, facecolor='blue', alpha=0.5)
plt.show()

在此输入图像描述

相似/相关: https//stackoverflow.com/questions/45580865/python-generate-an-n-dimensional-hypercube-using-rejection-sampling#comment78122144_45580865

Python在4维球面上的点的均匀分布

http://mathworld.wolfram.com/HyperspherePointPicking.html

在球形体积内采样均匀分布的随机点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM