4. Random coordinates#
Sometimes, we need to generate a random spread of points in 1 or more
dimensions. This can be used for random sampling methods or simply to
generate some synthetic data on which to test new methods. Generating
uniformly distributed values using numpy.random is great but doing
so in more than 1 dimension involves some boilerplate code that could be
automated. Bordado offers functions random_coordinates and
random_coordinates_spherical for this purpose. This is how they
work.
import bordado as bd
import matplotlib.pyplot as plt
import pygmt
4.1. Generating random points#
To generate uniformly distributed random 2D point coordinates, we use bordado.random_coordinates like so:
region = (0, 20, -45, -30)
coordinates = bd.random_coordinates(region, size=50)
for i, c in enumerate(coordinates):
print(f"coordinate {i}:\n{c}\n")
coordinate 0:
[14.75775571 6.59423122 9.76932479 10.63073862 3.15647507 9.03558484
18.89835081 19.86854838 6.60099574 9.26038478 13.97549729 10.36208642
5.11892063 10.01866189 15.06913483 14.61278974 12.37472421 15.06508227
2.2122211 18.27122259 10.85597985 4.00429569 15.74530925 17.43996662
4.57942855 18.81584818 17.8233206 0.28771781 17.64442378 2.74675639
15.50662678 11.65580557 1.62883121 12.63762956 8.432965 17.34389404
9.68867086 11.67937222 3.81819142 11.07457478 15.40520603 6.20606246
8.78953758 6.64827733 8.82784323 9.51923948 16.05588595 2.36090892
0.45929709 6.56125902]
coordinate 1:
[-31.155829 -39.80687992 -30.84831538 -36.3788026 -41.09866891
-44.88350999 -42.41004786 -42.16970372 -32.96240943 -44.77987114
-40.3769015 -35.67532289 -42.19428403 -42.42595201 -40.40124912
-34.7019997 -41.20362702 -37.06394233 -34.93054802 -43.9205636
-40.05898344 -31.40388806 -30.99966188 -33.51723894 -35.067534
-42.62898738 -30.91611511 -41.63107454 -31.55056256 -39.22044598
-33.77554578 -34.91691294 -43.71458207 -39.19374468 -40.14480805
-35.19396547 -32.79243094 -31.59368982 -40.78215611 -36.56282769
-30.6194835 -44.13973656 -30.52996428 -41.4396466 -34.85552116
-42.62212835 -31.51995782 -41.92259005 -34.02808684 -43.66716201]
Since the region has 4 elements, it is assumed that there are two dimensions
in our problem and so 2 coordinates arrays are generated. Because the points are
uniformly distributed, there is only a size argument and no spacing like in
bordado.grid_coordinates or bordado.line_coordinates.
Hint
The values above will be different every time we run the function because
it will instantiate a new numpy.random.default_rng every time. We
can control the randomness by passing a random_seed argument. This can
be an integer seed or a numpy.random.Generator.
Let’s plot these points to see their distribution:
fig, ax = plt.subplots(1, 1, figsize=(8, 6), layout="constrained")
ax.plot(*coordinates, "o")
ax.set_aspect("equal")
ax.set_xlim(*region[:2])
ax.set_ylim(*region[2:])
ax.set_xlabel("Easting")
ax.set_ylabel("Northing")
ax.grid()
plt.show()
As expected, they are random and with no bias inside the given region (hence, uniformly distributed).
4.2. Random points on the sphere#
The method above is useful, but it shouldn’t be used with geographic longitude and latitude coordinates. Let’s illustrate why with an example. We’ll generate random points distributed globally:
region = (0, 360, -90, 90) # W, E, S, N in degrees
size = 3000
coordinates = bd.random_coordinates(region, size)
fig, ax = plt.subplots(1, 1, figsize=(8, 6), layout="constrained")
ax.plot(*coordinates, ".")
ax.set_aspect("equal")
ax.set_xlim(*region[:2])
ax.set_ylim(*region[2:])
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
ax.grid()
plt.show()
Plotted like this, things seem fine. But this type of plot can be very misleading for geographic data. If I plot them on a map using a cartographic projection, a pattern will appear:
fig = pygmt.Figure()
fig.coast(land="#cccccc", region="g", projection="W20c", frame="afg")
fig.plot(x=coordinates[0], y=coordinates[1], style="c0.1c", fill="seagreen")
fig.show()
Now we can see that the concentration of points is larger at the poles than at the equator. This is because the meridians of longitude converge at the poles. Hence, a degree of longitude corresponds to a smaller and smaller physical size on the surface of the Earth towards the poles.
See also
The map above was generated with a Mollweide projection, which is an equal-area projection.
To generate random points on a sphere that have a uniform distribution, we
can use bordado.random_coordinates_spherical, which accounts for this
convergence of meridians:
coordinates_sphere = bd.random_coordinates_spherical(region, size)
fig = pygmt.Figure()
fig.coast(land="#cccccc", region="g", projection="W20c", frame="afg")
fig.plot(x=coordinates_sphere[0], y=coordinates_sphere[1], style="c0.1c", fill="seagreen")
fig.show()
Notice that now the point concentration is uniform on the Earth.
4.3. What’s next#
Now that we can make some synthetic data with random points, let’s see how we can split and segment the data based on spatial blocks and windows in “Splitting points into blocks”.
Have questions?
Please ask on any of the Fatiando a Terra community channels!