绘制不需要的三角形

2024-10-04 03:21:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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)

结果: enter image description here


Tags: 方法代码add图形plotmatplotlibfigplt
1条回答
网友
1楼 · 发布于 2024-10-04 03:21:03

我终于想办法了。下面是代码和获得的图形:

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

相关问题 更多 >