Python 将NumPy 2D阵列中的所有2D点连接为三角形网格
我对Python非常陌生,我正在尝试绘制一个三角形网格,如下所示:Python 将NumPy 2D阵列中的所有2D点连接为三角形网格,python,arrays,numpy,matplotlib,Python,Arrays,Numpy,Matplotlib,我对Python非常陌生,我正在尝试绘制一个三角形网格,如下所示: import matplotlib.pyplot as plt import numpy as np r = 0.25 d = 2*r s = 0 l1 = np.array([[s,0], [s+d,0], [s+2*d,0], [s+3*d,0]]) l2 = np.array([[s-r,d], [s+r,d], [s+r+d,d], [s+r+2*d,d]]) l3 = np.array([[s,2*d
import matplotlib.pyplot as plt
import numpy as np
r = 0.25
d = 2*r
s = 0
l1 = np.array([[s,0], [s+d,0], [s+2*d,0], [s+3*d,0]])
l2 = np.array([[s-r,d], [s+r,d], [s+r+d,d], [s+r+2*d,d]])
l3 = np.array([[s,2*d], [s+d,2*d], [s+2*d,2*d], [s+3*d,2*d]])
l4 = np.array([[s-r,3*d], [s+r,3*d], [s+r+d,3*d], [s+r+2*d,3*d]])
l5 = np.array([[s,4*d], [s+d,4*d], [s+2*d,4*d], [s+3*d,4*d]])
plt.scatter(*zip(*l1))
plt.scatter(*zip(*l2))
plt.scatter(*zip(*l3))
plt.scatter(*zip(*l4))
plt.scatter(*zip(*l5))
plt.show
我的问题是,我不知道如何连接所有的点。我用plt.plot(*zip(*l1))
为所有l
添加了水平线,但我不知道如何绘制“垂直”之字形线。。。有人有“简单”的解决方案吗
非常感谢 按照您的方式使用代码(或者根据您的需要查看triplot_演示,如@GBy所述),您可以提取或旋转每个数组,以便只向下绘制线:
import matplotlib.pyplot as plt
import numpy as np
r = 0.25
d = 2*r
s = 0
l1 = np.array([[s,0], [s+d,0], [s+2*d,0], [s+3*d,0]])
l2 = np.array([[s-r,d], [s+r,d], [s+r+d,d], [s+r+2*d,d]])
l3 = np.array([[s,2*d], [s+d,2*d], [s+2*d,2*d], [s+3*d,2*d]])
l4 = np.array([[s-r,3*d], [s+r,3*d], [s+r+d,3*d], [s+r+2*d,3*d]])
l5 = np.array([[s,4*d], [s+d,4*d], [s+2*d,4*d], [s+3*d,4*d]])
fig = plt.figure(0)
ax = fig.add_subplot(111)
larr = [l1,l2,l3,l4,l5]
# Plot horizontally
for l in larr:
# same as your *zip(*l1), but you can select on a column-wise basis
ax.errorbar(l[:,0], l[:,1], fmt="o", ls="-", color="black")
# Plot zig-zag-horizontally
for i in range(len(larr[0])):
lxtmp = np.array([x[:,0][i] for x in larr])
lytmp = np.array([x[:,1][i] for x in larr])
ax.errorbar(lxtmp, lytmp, fmt="o", ls="-", color="black")
ax.set_ylim([-0.1,2.1])
ax.set_xlim([-0.6,1.6])
plt.show()
编辑:
因此,x[:,0]表示获取所有行“:”但仅获取第一列“0”。对于l1,它将返回:
l1[:,0]
array([ 0. , 0.5, 1. , 1.5])
哪些是l1的x值。执行l1[:,1]将返回列“1”中的所有行,即y值。要绘制垂直线,您需要从每个第i个数组中获取所有的x和y值,然后循环所有数组,取出第i个元素。例如,第三条垂直之字形线是:
lxtmp = [l1[:,0][2], l2[:,0][2], l3[:,0][2], l4[:,0][2], l5[:,0][2]]
lytmp = [l1[:,1][2], l2[:,1][2], l3[:,1][2], l4[:,1][2], l5[:,1][2]]
为了简化和运行每个元素,我创建了“larr”来循环和“build”,然后以正常的python方式运行,例如
[i for i in range(1,10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
就是为了这个目的:绘制三角形。
您可以只传递x
和y
坐标(在这种情况下,将计算Delaunay三角剖分),也可以传递一个完整的三角剖分
对象,您可以为其指定自己的三角形
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as mtri
r = 0.25
d = 2*r
s = 0
def meshgrid_triangles(n, m):
""" Returns triangles to mesh a np.meshgrid of n x m points """
tri = []
for i in range(n-1):
for j in range(m-1):
a = i + j*(n)
b = (i+1) + j*n
d = i + (j+1)*n
c = (i+1) + (j+1)*n
if j%2 == 1:
tri += [[a, b, d], [b, c, d]]
else:
tri += [[a, b, c], [a, c, d]]
return np.array(tri, dtype=np.int32)
x0 = np.arange(4) * d
y0 = np.arange(5) * d
x, y = np.meshgrid(x0, y0)
x[1::2] -= r
triangles = meshgrid_triangles(4, 5)
triangulation = mtri.Triangulation(x.ravel(), y.ravel(), triangles)
plt.scatter(x, y, color='red')
plt.triplot(triangulation, 'g-h')
plt.show()
非常感谢您的回答!你能解释一下
lxtmp
和lytmp
是如何构建的吗?不幸的是我不习惯这种。。。阵列构建:)此外,我无法构建绘制“其他”之字形线的for循环,例如从(0,0)到(0.25,0.5)等。我更新了第一个问题的答案。关于第二个问题,最好使用@GBy的建议。我会尽快调查的。非常感谢@jonnybazookatone!这确实帮了大忙!:)谢谢@GBy,我接受了你的帖子作为答案,因为这三个字母让它变得很简单。因此,感谢您的代码和帮助!:)
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as mtri
r = 0.25
d = 2*r
s = 0
def meshgrid_triangles(n, m):
""" Returns triangles to mesh a np.meshgrid of n x m points """
tri = []
for i in range(n-1):
for j in range(m-1):
a = i + j*(n)
b = (i+1) + j*n
d = i + (j+1)*n
c = (i+1) + (j+1)*n
if j%2 == 1:
tri += [[a, b, d], [b, c, d]]
else:
tri += [[a, b, c], [a, c, d]]
return np.array(tri, dtype=np.int32)
x0 = np.arange(4) * d
y0 = np.arange(5) * d
x, y = np.meshgrid(x0, y0)
x[1::2] -= r
triangles = meshgrid_triangles(4, 5)
triangulation = mtri.Triangulation(x.ravel(), y.ravel(), triangles)
plt.scatter(x, y, color='red')
plt.triplot(triangulation, 'g-h')
plt.show()