Skip to content

Examples: Replace deprecated "np.random.seed()" with "np.random.Generator()" (NPY002 warning by ruff) #2781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 31, 2023
Merged
6 changes: 3 additions & 3 deletions examples/gallery/histograms/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import numpy as np
import pygmt

np.random.seed(100)

# Generate random elevation data from a normal distribution
rng = np.random.default_rng(seed=100)
mean = 100 # mean of distribution
stddev = 25 # standard deviation of distribution
data = mean + stddev * np.random.randn(521)
data = rng.normal(loc=mean, scale=stddev, size=521)


fig = pygmt.Figure()

Expand Down
9 changes: 5 additions & 4 deletions examples/gallery/histograms/scatter_and_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
import numpy as np
import pygmt

np.random.seed(19680801)

# Generate random data from a standard normal distribution centered on 0
x = np.random.randn(1000)
y = np.random.randn(1000)
# with a standard deviation of 1
rng = np.random.default_rng(seed=19680801)
x = rng.normal(loc=0, scale=1, size=1000)
y = rng.normal(loc=0, scale=1, size=1000)

# Get axis limits
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))


fig = pygmt.Figure()
fig.basemap(
region=[-xymax - 0.5, xymax + 0.5, -xymax - 0.5, xymax + 0.5],
Expand Down
7 changes: 4 additions & 3 deletions examples/gallery/symbols/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import pygmt

# Generate a random set of points to plot
np.random.seed(42)
rng = np.random.default_rng(seed=42)
region = [150, 240, -10, 60]
x = np.random.uniform(region[0], region[1], 100)
y = np.random.uniform(region[2], region[3], 100)
x = rng.uniform(low=region[0], high=region[1], size=100)
y = rng.uniform(low=region[2], high=region[3], size=100)


fig = pygmt.Figure()
# Create a 15 cm x 15 cm basemap with a Cartesian projection (X) using the
Expand Down
14 changes: 9 additions & 5 deletions examples/gallery/symbols/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@
import numpy as np
import pygmt

np.random.seed(19680801)
rng = np.random.default_rng(seed=19680801)
n = 200 # number of random data points

fig = pygmt.Figure()
fig.basemap(
region=[-0.1, 1.1, -0.1, 1.1],
region=[-1, 1, -1, 1],
projection="X10c/10c",
frame=["xa0.2fg", "ya0.2fg", "WSrt"],
frame=["xa0.5fg", "ya0.5fg", "WSrt"],
)
for fill in ["gray73", "darkorange", "slateblue"]:
x, y = np.random.rand(2, n) # random X and Y data in [0,1]
size = np.random.rand(n) * 0.5 # random size [0,0.5], in cm
# Generate standard normal distributions centered on 0
# with standard deviations of 1
x = rng.normal(loc=0, scale=0.5, size=n) # random x data
y = rng.normal(loc=0, scale=0.5, size=n) # random y data
size = rng.normal(loc=0, scale=0.5, size=n) * 0.5 # random size, in cm

# plot data points as circles (style="c"), with different sizes
fig.plot(
x=x,
Expand Down
6 changes: 3 additions & 3 deletions examples/tutorials/advanced/cartesian_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
# %%
# Generate random data from a normal distribution:

np.random.seed(100)
rng = np.random.default_rng(seed=100)

# Mean of distribution
mean = 100
# Standard deviation of distribution
stddev = 20

# Create two data sets
data01 = np.random.normal(mean, stddev, 42)
data02 = np.random.normal(mean, stddev * 2, 42)
data01 = rng.normal(loc=mean, scale=stddev, size=42)
data02 = rng.normal(loc=mean, scale=stddev * 2, size=42)


# %%
Expand Down