如何在设定的边界内放置随机均匀的点?

时间:2018-03-16 17:05:30

标签: python gis

我试图通过导入matplotlib和随机生成我生成的随机点。

我能够像这样生成我需要的点数:

upper = 0 lower = 500

for points in range(upper, lower):
    rand_lat = random.uniform(upper, lower)
    rand_lng = random.uniform(upper, lower)
    plt.plot(rand_lat, rand_lng, 'bo')

当我在这里运行时,结果是:

enter image description here

在我生成了这些点之后,我希望将它们包含在我为状态设置的边界内。我有从北,西,东,南设置的经度/纬度如下:

plt.plot( [-109.05,-102.05,-102.05,-109.05,-109.05], [41,41,37,37,41] )

但我需要它们适合所有在上面设置的状态边界内。那个州边界看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:1)

你实际上已经掌握了所需的一切。就这样做:

import random
import matplotlib.pyplot as plt
import matplotlib.patches as patches

n = 1000

xlo, xhi = -109, -102
ylo, yhi = 37, 41

x_rand = [random.uniform(xlo, xhi) for _ in range(n)]
y_rand = [random.uniform(ylo, yhi) for _ in range(n)]

ax = plt.figure().gca()
ax.scatter(x_rand, y_rand, s=5)
ax.add_patch(patches.Rectangle((xlo,ylo), abs(xlo-xhi), abs(yhi-ylo), linewidth=1, 
                               edgecolor='r', facecolor='none', linestyle='--'))
plt.show()

给你

enter image description here

从你的评论中我感觉到你正在寻找这样的东西:

import random
import matplotlib.pyplot as plt
import matplotlib.patches as patches

w = 1
n = 100
quadrants = [(x, y) for x in range(-1, 1) for y in range(-1, 1)]

ax = plt.figure(figsize=(8,8)).gca()

markers = ['x', '^', 'o', ',']

for marker, q in zip(markers, quadrants):

    xlo, ylo = q
    xhi, yhi = xlo+w, ylo+w

    x_rand = [random.uniform(xlo, xhi) for _ in range(n)]
    y_rand = [random.uniform(ylo, yhi) for _ in range(n)]

    ax.scatter(x_rand, y_rand, s=25, marker=marker)    
    ax.add_patch(patches.Rectangle((xlo,ylo), abs(xlo-xhi), abs(yhi-ylo), linewidth=1, 
                               edgecolor='k', facecolor='none', linestyle='--'))

enter image description here

相关问题