Sidney Lee
Sidney Lee

Reputation: 35

Matplotlib Triangulation: more triangles than vertices

I want to create an unstructured triangular grid, and I have two files: for vertices and triangles. The problem is that Matplotlib Triangulation method requires the length of array of vertices and the length of array of triangles being same, but my data has more triangles than vertices. So, instead of full grid I can get only part of it (skipping the tail of triangles array).

My output:

Grid I want to complete

Expected output:

Expected output

Vertices array:

[[0.         0.        ]
 [0.         1.        ]
 [1.         0.        ]
 [1.         1.        ]
 [0.         0.33333333]
 [0.         0.66666667]
 [0.33333333 0.        ]
 [0.66666667 0.        ]
 [0.33333333 1.        ]
 [0.66666667 1.        ]
 [1.         0.33333333]
 [1.         0.66666667]
 [0.48987551 0.53023763]
 [0.43898046 0.76879765]
 [0.70260457 0.70541771]
 [0.23823447 0.57580349]
 [0.30488903 0.35751012]
 [0.7335183  0.42800646]
 [0.52102626 0.34589035]
 [0.80431355 0.19597677]
 [0.19985633 0.8004077 ]
 [0.44618858 0.18491763]
 [0.84231781 0.84302109]
 [0.21688219 0.17515222]
 [0.63434267 0.23095824]]

Triangles array:

[(4, 0, 23), (0, 6, 23), (6, 21, 23), (6, 7, 21), (21, 16, 23), (18, 16, 21), (12, 16, 18), (12, 15, 16), (15, 4, 16), (16, 4, 23), (5, 4, 15), (5, 15, 20), (1, 5, 20), (8, 1, 20), (13, 8, 20), (9, 8, 13), (15, 13, 20), (12, 13, 15), (14, 12, 17), (13, 12, 14), (9, 13, 14), (9, 14, 22), (3, 9, 22), (11, 3, 22), (14, 11, 22), (11, 14, 17), (10, 11, 17), (10, 17, 19), (17, 12, 18), (18, 21, 24), (17, 18, 24), (19, 17, 24), (7, 19, 24), (21, 7, 24), (7, 2, 19), (2, 10, 19)]

Code I wrote:

nodes = np.asarray(convert(read_file("d2.txt"), float))
x, y = np.degrees(nodes[:, 0]), np.degrees(nodes[:, 1])
v_numbers = np.asarray(convert(read_file("d1.txt"), int))
triang = Triangulation(x, y, v_numbers[0:25])
plt.figure()
plt.gca().set_aspect('equal')
plt.triplot(triang, 'go-')
plt.show()

Is there any way to deal with this, or other library exists with methods allowing to get what I want?

Upvotes: 2

Views: 592

Answers (1)

Derek O
Derek O

Reputation: 19565

You don't have to provide the indices of the triangles: by default, matplotlib can automatically generate them using a Delaunay triangulation (see the documentation), and this should be sufficient for your data.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation

nodes = np.array([[0.,         0.        ],
 [0.,        1.        ],
 [1.,         0.        ],
 [1.,         1.        ],
 [0.,         0.33333333],
 [0.,         0.66666667],
 [0.33333333, 0.        ],
 [0.66666667, 0.        ],
 [0.33333333, 1.        ],
 [0.66666667, 1.        ],
 [1.,         0.33333333],
 [1.,         0.66666667],
 [0.48987551, 0.53023763],
 [0.43898046, 0.76879765],
 [0.70260457, 0.70541771],
 [0.23823447, 0.57580349],
 [0.30488903, 0.35751012],
 [0.7335183,  0.42800646],
 [0.52102626, 0.34589035],
 [0.80431355, 0.19597677],
 [0.19985633, 0.8004077 ],
 [0.44618858, 0.18491763],
 [0.84231781, 0.84302109],
 [0.21688219, 0.17515222],
 [0.63434267, 0.23095824]])

x, y = np.degrees(nodes[:, 0]), np.degrees(nodes[:, 1])
triang = Triangulation(x, y)
plt.figure()
plt.gca().set_aspect('equal')
plt.triplot(triang, 'go-')
plt.show()

enter image description here

Upvotes: 3

Related Questions