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

Python 如何在设定的边界内随机放置均匀的点?,python,gis,Python,Gis,我试图将通过导入matplotlib和random生成的随机点放在一起 我能够生成我需要的点数,如下所示: 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') import ran

我试图将通过导入matplotlib和random生成的随机点放在一起

我能够生成我需要的点数,如下所示:

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')
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()
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='--'))
当我运行它时,以下是结果:

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

plt.plot( [-109.05,-102.05,-102.05,-109.05,-109.05], [41,41,37,37,41] )
但我需要它们都符合上面设定的状态边界。该州边界如下所示:

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

实际上,你手头已经有了所需的一切。就这样做吧:

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')
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()
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='--'))
这给了你


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

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

鉴于“状态”边界严格为矩形,这是一个合适的答案,这是唯一要求的情况。如果几何图形发生变化,则需要以不同方式生成数字。谢谢!我还试图将其限制在特定的象限,中间经纬度将象限分隔为:(-105.50,39)。到目前为止,我得到的是:Q1=plt.plot([-105.5,-102.05,-102.05,-105.5,-105.5,-105.5,-41,41,39,39,41])-------------------------------------Q2=plt.plot([-109.05,-105.5,-109.05,-109.05],[41,41,39,39,41]),我想我知道了。你介意检查一下我在这方面的工作吗:plt.plot([-109.05,-102.05,-102.05,-109.05,-109.05],[41,41,37,37,41])Q1=plt.plot([-105.5,-102.05,-102.05,-105.5,-105.5,-105.5,-109.05,-109.05],[41,41,39,41])Q3=plt.plt.plt.plt([-109.05,--109.05,--109.05],-Q3=[-109.05,-109.05,-105.5,-105.5,-109.05],[37,39,39,37,37])-------------------------------------Q4=plt.图([-105.5,-105.5,-102.05,-105.5],[39,39,39,37,37])在这些四边形被隔离的情况下,我如何才能将不同的形状填充到这些区域中?@SoscratesFd不太确定我是否理解,但是..你总是基于两点绘制一个矩形
(xlo,ylo)
(xhi,yhi)
在我们的例子中。您所要做的就是调整这些值并更改要从中提取样本的矩形。或者这里有什么问题?