Skip to main content
 首页 » 编程设计

python之plot_trisurf 不需要的三角形

2025年04月02日18powertoolsteam

我正在使用 python 并使用 matplotlib 创建 3D 图形,但是 plot_trisurf 方法将三角形保留在图形的一个边缘上,这真的很烦人。我一直在寻找解决方案,但没有成功解决问题。 这是代码:

variableToDisplay='U' 
 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
 
surf = ax.plot_trisurf(X,Y,Z,cmap=cm.jet, linewidth=0.1)    
fig.colorbar(surf)   
fig.tight_layout()   
plt.show(block=False) 

结果:

请您参考如下方法:

我终于找到方法了。这是代码和获得的图表:

from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 
import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.tri as tri 
from matplotlib.ticker import MaxNLocator 
import os 
 
 
isbad =np.greater(y, 4.88) 
triang = tr.Triangulation(x, y) 
mask = np.all(np.where(isbad[triang.triangles], True, False), axis=1) 
triang.set_mask(mask) 
 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
surf = ax.plot_trisurf(triang,Z,cmap=cm.jet, linewidth=0.1) 
fig.colorbar(surf) 
fig.tight_layout() 
ax.set_xlabel('Position of debond (% of blade length)') 
ax.set_ylabel('Size of debond') 
ax.set_zlabel(variableToDisplay) 
plt.show(block=False) 

在变量“isbad”中,4.88 几乎指的是 y 列表的最大值,因此将避免该边上的三角形

enter image description here