将NumPy二维数组中的所有二维点连接成三角网格

3 投票
2 回答
3067 浏览
提问于 2025-04-18 12:00

我刚接触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], [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加了水平线,但我不知道怎么画那些“竖着”的锯齿线……有没有人能给我一个“简单”的解决办法?

非常感谢!

2 个回答

1

按照你使用的代码方式(如果你想要其他效果,可以参考triplot_demo,正如@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()

plot image

补充说明:

lxtmp = np.array([x[:,0][i] for x in larr])

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'来循环,并以普通的Python方式“构建”,比如:

[i for i in range(1,10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
2

triplot 是用来画三角形的工具。你可以只提供 xy 坐标(这样程序会自动计算出三角形的形状),或者你也可以提供一个完整的 Triangulation 对象,这样你就可以自己指定想要的三角形。

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()

enter image description here

撰写回答