我试图生成一个图,它有一个随机分布,在Python中给定数量的单位之后重复。
换句话说,我有一个随机分布点的区域。我想平铺一个平面与相同的副本,以使相同的点可以看到在一个特定的距离,类似于下面的图片(注意红色图案)。
我有随机分布点的代码,我只是不知道如何重复(或平铺)这个图。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
n = 3 #Number of random points
a = np.random.uniform(-20, 20, size=(n,2))
plt.scatter(a[:,0],a[:,1])发布于 2022-10-17 20:30:27
你只想在每个瓷砖上重复你的主题。下面我只是使用一组嵌套的for循环,但是使用一个广播 np.meshgrid可能更惯用。
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(3)
# number of points in motif and random background points
n_motif = 7
n_bg = 500
# generate a motif
px, py = map(np.sort, np.random.uniform(-1, 1, size=[2, n_motif]))
px -= px.mean()
py -= py.mean()
# create figure
fig, ax = plt.subplots(figsize=(6,6))
# generate tiling of motif and plot
tiles = np.linspace(-10, 10, 5)
for x in tiles:
for y in tiles:
ax.plot(px + x, py + y, '.-', color='C3')
# add some background points inside a circle
rr = 15 * np.sqrt(np.random.random(n_bg))
rc = np.random.uniform(-np.pi, np.pi, n_bg)
ax.scatter(np.cos(rc) * rr, np.sin(rc) * rr, 5, color='#333')
# fix aspect ratio so circle doesn't turn into an ellipse
ax.set_aspect(1)

https://stackoverflow.com/questions/74102263
复制相似问题