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

如何解决Matplotlib 3D曲面图Z轴溢出问题?

3D曲面图Z轴范围约束失效问题

我用Matplotlib绘制3D网格曲面时,X、Y轴范围能正确约束,但Z轴设置set_zlim3d后还是出现溢出。代码如下:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10,10))

x = np.linspace(-6,6,100)
y = np.linspace(-6,6,100)
X,Y = np.meshgrid(x,y)

def f(x,y):
    return x**2 + 3*y

Z = f(X,Y)


ax = plt.axes(projection = '3d')
ax.plot_surface(X,Y,Z,cmap='viridis')

ax.title.set_text("z=x**2+3y")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

ax.set_zlim3d(zmin=-3,zmax=5)
ax.set_xlim3d(xmin=-6,xmax=6)
ax.set_ylim3d(ymin=-6,ymax=6)

plt.show()

我试过两种方式裁剪Z值:

z_tmp = np.maximum(np.minimum(5,Z),-3)
z_temp = np.clip(Z, -3, 5, None)

但裁剪会把超出边界的Z值直接设为边界值,视觉效果很差,怀疑是不是Mac系统的问题?


解决方案

1. 调整轴范围设置顺序

Matplotlib的3D绘图逻辑中,默认会先根据数据自动适配轴范围,后续手动设置的范围可能被覆盖。先设置轴范围,再绘制曲面,能让绘图函数提前适配约束规则,避免Z轴溢出:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10,10))
ax = plt.axes(projection='3d')

# 先设定所有轴范围
ax.set_xlim3d(xmin=-6,xmax=6)
ax.set_ylim3d(ymin=-6,ymax=6)
ax.set_zlim3d(zmin=-3,zmax=5)

x = np.linspace(-6,6,100)
y = np.linspace(-6,6,100)
X,Y = np.meshgrid(x,y)

def f(x,y):
    return x**2 + 3*y

Z = f(X,Y)

# 再绘制曲面
ax.plot_surface(X,Y,Z,cmap='viridis')

ax.title.set_text("z=x**2+3y")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

2. 过滤无效网格点绘制

如果不想调整顺序,可以直接过滤掉Z值超出[-3,5]的网格点,只保留有效区域绘制:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10,10))

x = np.linspace(-6,6,100)
y = np.linspace(-6,6,100)
X,Y = np.meshgrid(x,y)

def f(x,y):
    return x**2 + 3*y

Z = f(X,Y)

# 生成有效区域掩码,过滤超出Z范围的点
mask = (Z >= -3) & (Z <= 5)
X_filtered = X[mask]
Y_filtered = Y[mask]
Z_filtered = Z[mask]

ax = plt.axes(projection='3d')
# 用三角曲面绘制非规则网格的有效区域
ax.plot_trisurf(X_filtered, Y_filtered, Z_filtered, cmap='viridis')

ax.title.set_text("z=x**2+3y")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

ax.set_zlim3d(zmin=-3,zmax=5)
ax.set_xlim3d(xmin=-6,xmax=6)
ax.set_ylim3d(ymin=-6,ymax=6)

plt.show()

关于Mac系统的疑问

Mac系统本身不会导致set_zlim3d失效,问题核心是Matplotlib 3D绘图的渲染逻辑,调整设置顺序即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 19:27:34