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

VSCode中Jupyter Notebook的3D散点图Z轴标签截断问题求助

VSCode Jupyter Notebook中Matplotlib 3D散点图Z轴标签被截断问题

问题详情

  • 在VSCode内置Jupyter Notebook绘制3D散点图时,Z轴标签出现显示截断的异常
  • 运行Matplotlib官方3D散点图示例代码仍可复现该问题
  • 已尝试的方案及结果:
    • %matplotlib qt:可解决截断,但会弹出独立窗口,不符合批量绘图需求
    • %matplotlib widget/%matplotlib notebook/%matplotlib ipympl:无法正常显示绘图
    • 调整Z轴位置:无效果
  • 该问题在Jupyter Lab中不存在,已向VSCode和Matplotlib的GitHub仓库提交Issue

测试代码

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


def randrange(n, vmin, vmax):
    """
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    """
    return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

n = 100

# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

临时解决办法

可以通过手动调整子图边距,为Z轴标签预留足够显示空间,在plt.show()前添加以下代码:

# 调整右侧、底部边距,避免标签截断
fig.subplots_adjust(right=0.8, bottom=0.15)

或者使用tight_layout()并设置pad参数,自动调整布局:

plt.tight_layout(pad=3.0)

以上方法可在保持内嵌绘图的前提下解决标签截断问题。

内容的提问来源于stack exchange,提问作者David Siret Marquès

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 11:57:20