Reputation: 49
I am trying to put random dots I generated through the imports matplotlib and random.
I am able to generate the number of dots I need like this:
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')
When I run it here are the results:
After I've generated those points I want to contain them within set boundaries I made for the state. I have the longitude/latitudes set up from North, West, East, South as follows:
plt.plot( [-109.05,-102.05,-102.05,-109.05,-109.05], [41,41,37,37,41] )
But I need them to fit all inside a state boundary as set above. That state boundary looks like this:
Upvotes: 0
Views: 335
Reputation: 25407
You actually have everything you need at hand already. Just do it like this:
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()
which gives you
From your comments I sense you're looking for something like this:
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='--'))
Upvotes: 1