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

如何在Matplotlib子图中绘制3D图并移除多余标签

问题解决:移除Matplotlib 3×3子图中3D子图的多余标签层

问题原因

你通过plt.subplots(3,3)创建了9个2D子图,之后用fig.add_subplot(333, projection='3d')在(0,2)位置新增了3D子图,但原有的2D子图并未被删除,两个子图重叠显示,导致出现多余的标签层。

解决方案

有两种简洁的修复方式:

方法1:先删除原有2D子图,再添加3D子图

在创建3D子图前,调用remove()方法删除原(0,2)位置的子图:

fig, ax = plt.subplots(3,3,figsize=(30, 30))

#Trajectory
tau=np.linspace(0,1,n)
ax[0,0].plot(tau,x)
ax[0,0].set_title('1D trajectory')
ax[0,0].set_xlabel('\u03C4')
ax[0,0].set_ylabel('x')

ax[0,1].plot(x,y)
ax[0,1].set_title('2D trajectory')
ax[0,1].set_xlabel('x')
ax[0,1].set_ylabel('y')

# 先删除原有(0,2)的2D子图
ax[0,2].remove()
# 再添加3D子图
ax[0,2] = fig.add_subplot(333, projection='3d')
ax[0,2].plot(x, y, z)

ax[0,2].set_title('3D trajectory')
ax[0,2].set_xlabel('x')
ax[0,2].set_ylabel('y')
ax[0,2].set_zlabel('z')

#MSD-Simulation
ax[1,0].plot(tm,dr1,marker='o',linestyle='none',mec='brown',mfc='none',markevery=1)
ax[1,0].set_title('1D MSD')
ax[1,0].set_xlabel('\u03C4')
ax[1,0].set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

ax[1,1].plot(tm,dr2,marker='o',linestyle='none',mec='grey',mfc='none',markevery=1)
ax[1,1].set_title('2D MSD')
#ax[1,1].set_xlabel('\u03C4')
#ax[1,1].set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

ax[1,2].plot(tm,dr3,marker='o',linestyle='none',mec='black',mfc='none',markevery=1)
ax[1,2].set_title('3D MSD')
#ax[1,2].set_xlabel('\u03C4')
#ax[1,2].set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

#MSD-THEORITICALL
ax[2,0].plot(tm,M1,'brown')
ax[2,0].set_title('1D MSD')
ax[2,0].set_xlabel('\u03C4')
ax[2,0].set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

ax[2,1].plot(tm,M2,'grey')
ax[2,1].set_title('2D MSD')
#ax[2,1].set_xlabel('\u03C4')
#ax[2,1].set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

ax[2,2].plot(tm,M3,'black')
ax[2,2].set_title('3D MSD')
#ax[2,2].set_xlabel('\u03C4')
#ax[2,2].set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

plt.tight_layout()

方法2:初始化时直接指定3D子图(更推荐)

使用gridspec直接创建对应投影类型的子图,避免重复创建:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(30, 30))
# 创建3×3布局的网格
gs = fig.add_gridspec(3, 3)

# 1D轨迹子图
ax00 = fig.add_subplot(gs[0,0])
tau=np.linspace(0,1,n)
ax00.plot(tau,x)
ax00.set_title('1D trajectory')
ax00.set_xlabel('\u03C4')
ax00.set_ylabel('x')

# 2D轨迹子图
ax01 = fig.add_subplot(gs[0,1])
ax01.plot(x,y)
ax01.set_title('2D trajectory')
ax01.set_xlabel('x')
ax01.set_ylabel('y')

# 直接创建3D轨迹子图到(0,2)位置
ax02 = fig.add_subplot(gs[0,2], projection='3d')
ax02.plot(x, y, z)
ax02.set_title('3D trajectory')
ax02.set_xlabel('x')
ax02.set_ylabel('y')
ax02.set_zlabel('z')

# MSD-Simulation 子图
ax10 = fig.add_subplot(gs[1,0])
ax10.plot(tm,dr1,marker='o',linestyle='none',mec='brown',mfc='none',markevery=1)
ax10.set_title('1D MSD')
ax10.set_xlabel('\u03C4')
ax10.set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

ax11 = fig.add_subplot(gs[1,1])
ax11.plot(tm,dr2,marker='o',linestyle='none',mec='grey',mfc='none',markevery=1)
ax11.set_title('2D MSD')

ax12 = fig.add_subplot(gs[1,2])
ax12.plot(tm,dr3,marker='o',linestyle='none',mec='black',mfc='none',markevery=1)
ax12.set_title('3D MSD')

# MSD-THEORITICALL 子图
ax20 = fig.add_subplot(gs[2,0])
ax20.plot(tm,M1,'brown')
ax20.set_title('1D MSD')
ax20.set_xlabel('\u03C4')
ax20.set_ylabel("MSD(\u03C4) [$\u03BCm^2$]")

ax21 = fig.add_subplot(gs[2,1])
ax21.plot(tm,M2,'grey')
ax21.set_title('2D MSD')

ax22 = fig.add_subplot(gs[2,2])
ax22.plot(tm,M3,'black')
ax22.set_title('3D MSD')

plt.tight_layout()

效果说明

两种方法都能彻底移除重叠的2D子图标签,只保留3D子图的正常显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 17:33:10