You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python matplotlib 3D绘图嵌入2D图像叠加3D数据的问题排查

Matplotlib 3D坐标轴正交平面嵌入真实图片实现问题

实现目标

  • 基于Python完成三维空间数据可视化
  • 在三个正交坐标平面上放置对应方向的装置几何真实图片(非坐标点、颜色映射类数据):
    • XY平面z=0位置放置俯视图
    • XZ、YZ平面对应位置放置对应方向投影视图

已完成的实现逻辑

  • 初始方案尝试调用imshow方法搭配zdir参数映射图片到3D平面,发现该方法不存在zdir参数,方案不可行
  • 后续调整实现方案,通过plot_surface方法搭配facecolors参数编写plot_image工具函数完成图片嵌入,核心处理逻辑:
    • 新增val参数指定图片放置位置对应的轴坐标值
    • 传入xlim、ylim参数完成坐标轴范围适配
    • 对imread读取的图像数组做swapaxes轴交换、y轴翻转处理,匹配3D坐标方向

现存待解决问题

  • 渲染速度极慢:分辨率为3000*5000像素的图像使用普通imshow绘制仅需数秒,但使用plot_surface绘制时,即便将rstride、cstride参数设置为15(即采样步长放大15倍,画质对应下降15²倍),绘制3张图片仍需2分钟,暂不清楚imshow与plot_surface渲染性能差异巨大的原因
  • 图层层级异常:即便将图片的zorder参数设置为-1000,且先绘制背景图片再绘制3D数据,图片仍会错误遮挡前景的3D圆柱体数据,而3D数据的所有取值均不低于代码中设置的-144阈值
  • 渲染光照效果不符合预期,存在显示异常

核心实现代码

def plot_image(ax, image, axis, xlim, ylim, val, rstride=15, cstride=15):
    array = plt.imread(image)
    array = np.swapaxes(array, 0, 1)
    array = np.flip(array, 1)
    step_x, step_y = np.diff(xlim) / array.shape[0], np.diff(ylim) / array.shape[1]
    x_1 = np.arange(xlim[0], xlim[1], step_x)
    y_1 = np.arange(ylim[0], ylim[1], step_y)
    y_1, x_1 = np.meshgrid(y_1, x_1)
    vals = np.ones((array.shape[0], array.shape[1]))*val
    if axis == "x":
        ax.plot_surface(vals, x_1, y_1, rstride=rstride, cstride=cstride, facecolors=array, zorder=-1000)
    elif axis == "y":
        ax.plot_surface(x_1, vals, y_1, rstride=rstride, cstride=cstride, facecolors=array, zorder=-1000)
    elif axis == "z":
        ax.plot_surface(x_1, y_1, vals, rstride=rstride, cstride=cstride, facecolors=array,zorder=-1000)

fig = plt.figure(figsize=(10,15))
fig.add_subplot(111, projection="3d")

# 绘制背景几何结构
plot_image(plt.gca(), '/home/yryves/serpent_cases/domain_decomposition_dvlpt/test_stl2/Plots/input_geom1_bu1.png','x', xlim=[-144,144], ylim=[0,487.528], val=-144)
plot_image(plt.gca(), '/home/yryves/serpent_cases/domain_decomposition_dvlpt/test_stl2/Plots/input_geom2_bu1.png','y', xlim=[-144,144], ylim=[0,487.528], val=144)
plot_image(plt.gca(), '/home/yryves/serpent_cases/domain_decomposition_dvlpt/test_stl2/Plots/input_geom3_bu1.png','z', xlim=[-144,144], ylim=[-144,144], val=0)

# 绘制3D数据
pbed.plot3D('z', new_fig=False)

plt.savefig('geometry.png')

内容的提问来源于stack exchange,提问作者yvrob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 23:09:24