我想使用MATPLOTLIB绘制3D有限元网格。我在使用MAYAVI时遇到了一些问题,因此this类解决方案不适用。
我需要类似Matlab的“补丁”功能,如下所示:
x = [0.0, 1.0, 0.0];
y = [0.0, 0.0, 1.0];
z = [0.0, 1.0, 1.0];
v = 100.0;
view(3)
patch(x, y, z, v)
hold on;
x = [1.0, 1.0, 0.0];
y = [0.0, 1.0, 1.0];
z = [1.0, 0.0, 1.0];
v = 50.0;
patch(x, y, z, v)
答案 0 :(得分:1)
以下代码会产生相似的结果,但是您需要使用一些参数(颜色图,归一化范围(vmin,vmax),视角,轴刻度位置)来获得所需的确切信息:>
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
def patch(ax, x, y, z, v, vmin=0, vmax=100, cmap_name='viridis'):
cmap = mpl.cm.get_cmap(cmap_name) # Get colormap by name
c = cmap(mpl.colors.Normalize(vmin, vmax)(v)) # Normalize value and get color
pc = Poly3DCollection([list(zip(x,y,z))]) # Create PolyCollection from coords
pc.set_facecolor(c) # Set facecolor to mapped value
pc.set_edgecolor('k') # Set edgecolor to black
ax.add_collection3d(pc) # Add PolyCollection to axes
return pc
def view(ax, code):
if code == 2: #view(2) sets the default two-dimensional view, az = 0, el = 90.
ax.view_init(90, 0) # (args are reversed from MATLAB)
if code == 3: #view(3) sets the default three-dimensional view, az = –37.5, el = 30.
ax.view_init(30, -37.5)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [0.0, 1.0, 0.0];
y = [0.0, 0.0, 1.0];
z = [0.0, 1.0, 1.0];
v = 100.0;
patch(ax, x, y, z, v)
x = [1.0, 1.0, 0.0];
y = [0.0, 1.0, 1.0];
z = [1.0, 0.0, 1.0];
v = 50.0;
patch(ax, x, y, z, v)
view(ax, 3)
plt.show()
产生:
(答案顶部的图像被旋转以获得与您在问题中相似的视图,因为看起来有些不对劲。)