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

如何移除Matplotlib三维曲面图上的方格?

问题:移除Matplotlib 3D曲面的方格线

使用以下Matplotlib代码绘制3D函数后,曲面上出现了方格线,尝试关闭网格、隐藏刻度都无法消除,请问如何移除这些方格?

import numpy as np
import matplotlib.pyplot as plt
L = 10
a = 0.4
type = "COS"
fig = plt.figure(figsize = (12,10))
ax = plt.axes(projection='3d')
ax.grid(False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])

x = np.arange(0, L, 0.05)
y = np.arange(0, 20, 0.05)

X, Y = np.meshgrid(x, y)
def f(x,t,n):
    return np.cos(n*np.pi*x/L)*np.exp(-a*n*n*np.pi*np.pi*a*t/(L*L))


def s(n):
    if type == "PAR":
        if n > 0:
            return L*L*(4*np.pi*n+4*np.pi*n*((-1)**n))/(2*(np.pi**3)*(n**3))
        else:
            return L*L/12
    elif type == "COS":
        if n > 0:
            return 2*(L*np.sin(L)*((-1)**n))/(L*L-np.pi*np.pi*n*n)
        else:
            return np.sin(L)/L


def g(x,t):
    ans = s(0)
    for n in range(1,200):
        ans += s(n)*f(x,t,n)
    return ans


Z = g(X,Y)

surf = ax.plot_surface(X, Y, Z, cmap = plt.cm.inferno)
ax.set_xlabel('x', labelpad=20)
ax.set_ylabel('t', labelpad=20)
ax.set_zlabel('T', labelpad=20)
plt.show()

解决方案

你看到的方格不是坐标轴网格,而是3D曲面的边缘线,默认情况下plot_surface会绘制每个小面的边缘,因此需要在调用该函数时显式关闭这些边缘线:

  • 方法1:添加edgecolor='none'参数,直接隐藏边缘线
  • 方法2:添加linewidth=0参数,将边缘线宽度设为0

修改后的plot_surface代码行示例:

surf = ax.plot_surface(X, Y, Z, cmap=plt.cm.inferno, edgecolor='none')

替换原代码中的对应行后,绘制出的3D曲面就不会有方格线了。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 15:32:15