I am trying to plot a surface created with >10000 unstructured triangles. I have the coordinates of the triangle points and the each triangle points list. My data is as follows,
0.1 0.2 0.1
0.2 0.4 0.6
0.4 0.6 0.4
.
.
.
1 2 3
.
.
.
The first three lines are coordinates (-X,Y,Z COORDINATES-) of the points (point 1 in line 1, point 2 in line 2 and etc). The number of points are more than 10000. The "1 2 3" says that we have a triangle in which its corner points are 1, 2 and 3. So, I want to plot the surface by starting from the 1st triangle and plotting them one by one. I have tried to follow the above procedure but I do not get the right figure and finally I get the following error message.
Figure size 432x288 with 0 Axes
I have tried the following code.
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
# from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
fileName = open('surface.txt','r')
print(fileName.readline())
dummy = fileName.readline().split()
npo = int(dummy[2])
nel = int(dummy[4])
xp = np.zeros([npo])
yp = np.zeros([npo])
zp = np.zeros([npo])
el1 = np.zeros([nel])
el2 = np.zeros([nel])
el3 = np.zeros([nel])
for i in range(0,npo):
dummy = fileName.readline().split()
xp[i] = float(dummy[0])
yp[i] = float(dummy[1])
zp[i] = float(dummy[2])
# print(i,xp[i],yp[i],zp[i])
for i in range(0,nel):
dummy = fileName.readline().split()
el1[i] = int(dummy[0])
el2[i] = int(dummy[1])
el3[i] = int(dummy[2])
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection='3d')
for i in range(0,nel):
x1 = xp[int(el1[i])-1]
y1 = yp[int(el1[i])-1]
z1 = zp[int(el1[i])-1]
x2 = xp[int(el2[i])-1]
y2 = yp[int(el2[i])-1]
z2 = zp[int(el2[i])-1]
x3 = xp[int(el3[i])-1]
y3 = yp[int(el3[i])-1]
z3 = zp[int(el3[i])-1]
xarr = [x1,x2,x3,x1]
yarr = [y1,y2,y3,y1]
zarr = [z1,z2,z3,z1]
verts = [list(zip(xarr,yarr,zarr))]
ax2.add_collection3d(Poly3DCollection(verts))
ax2.set_xbound(0,1)
ax2.set_ybound(0,1)
ax2.set_zbound(0,3)
I will appreciate to hear your opinion.
The function plo_trisurf
does exactly what you want.
x, y, z
are the nodes of your triangles tri
containes the indices of your triangle nodes A small example:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.array([0, -1, 1, 1])
y = np.array([0, 1, -1, 1])
z = np.array([0, 1, 1, -1])
tri = np.array([[0, 1, 2],
[0, 1, 3],
[0, 2, 3]])
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_trisurf(x, y, z, triangles=tri)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.