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

Matplotlib绘制非标准函数z=f(x,y)曲面及Spyder运行报错解决

报错原因
  • 版本不兼容:该报错与绘图逻辑无关,是当前安装的Spyder版本未适配matplotlib新增的save_all接口参数导致的冲突,你的代码在终端可正常运行也验证了代码本身逻辑无问题。
  • 代码转义错误:你粘贴的代码中包含HTML转义字符未还原,直接运行会触发语法错误。
修复方案

第一步:解决版本兼容问题,任选一种方案即可

  • 升级适配版本:执行以下命令升级Spyder和matplotlib到兼容的最新稳定版:
    pip install --upgrade spyder matplotlib
  • 更换绘图后端:打开Spyder → 顶部菜单栏「工具」→ 「偏好设置」→ 「IPython控制台」→ 「图形」选项卡,将后端改为TkAgg,重启IPython内核后生效。

第二步:修正代码中的转义字符错误,修正后的完整可运行代码如下

#import pdb # for debugger
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import math as math


def agm(x0, y0): 
# return AGM 
    xagm_n = (x0 + y0)/2
    yagm_n = math.sqrt(x0*y0)        
    convagm = abs(xagm_n-yagm_n)        
    if (convagm < 1e-8):
        return xagm_n 
    else:
        return agm(xagm_n,yagm_n) 


def magm(half_na, half_nb, start_p=0.): 
# start_p is minimum 0; but could be choosen as the smallest of half_nx parameter
#   when it has to be initiated. perhaps it speeds the convergence.
# return MAGM
    xmagm_n = (half_na+half_nb)/2
    ymagm_n = start_p + math.sqrt((half_na-start_p)*(half_nb-start_p))
    zmagm_n = start_p - math.sqrt((half_na-start_p)*(half_nb-start_p))
    convmagm = abs(xmagm_n-ymagm_n)
    if (convmagm < 1e-10):
        return xmagm_n
    else:
        return magm(xmagm_n,ymagm_n,zmagm_n) 


def perim(x0, y0): 
    if (x0==0.) or (y0==0.):
        Result=4.*(x0+y0)
    else:
        Result=2.*math.pi*magm(x0**2,y0**2)/agm(x0,y0)
    return Result  

#pdb.set_trace()  # start the debugger

N = 50
Wide = 10.
X = np.arange(0.,Wide,Wide/N)
Y = np.arange(0.,Wide,Wide/N)
Z=np.zeros((N, N))
for i in range(N):
    for j in range(N):
        Z[i,j]=perim((Wide/N)*i, (Wide/N)*j)
X, Y = np.meshgrid(X, Y)
# fourth dimention - colormap
# create colormap according to x-value (can use any 50x50 array)
color_dimension = Z # change to desired fourth dimension
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors = m.to_rgba(color_dimension)
# plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf=ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, alpha=0.5, vmin=minn, vmax=maxx, shade=False)
ax.set_xlabel('ellipse halfparam a')
ax.set_ylabel('ellipse halfparam b')
ax.set_zlabel('ellipse perim')
Windo= Wide*2*math.pi
ax.set_zlim(0, Windo)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.01f'))
fig.colorbar(surf, shrink=0.5, aspect=10)
ax.contour(X, Y, Z, 10, linewidths=1.5, cmap="autumn_r", linestyles="solid", offset=-0.1)
ax.contour(X, Y, Z, 10, linewidths=1.5, colors="k", linestyles="solid")
ax.view_init(elev=20., azim=-100.)
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 12:36:03