matplotlib 中的曲面图使用函数 z = f(x,y),其中 f 不能用标准函数编写。如何?

问题描述 投票:0回答:0

我做了一个绘制表面的脚本。 X Y 是用规则的网格制作的。 Z 将是 X 和 Y 位置 f(X,Y) 的结果,其中 f 来自无限级数计算或收敛计算(因此,没有 Exp Sin **2 + .. 在用于描述函数的单个公式中)。 它不是从 Spyder 开始的。但是在终端工作。

此命令执行失败,因为在尝试从 Spyder 的编辑器中获取文件代码时发生错误。错误是:

发生异常,使用 %tb 查看完整回溯。

TypeError: handle_get_file_code() 有一个意外的关键字参数 'save_all'

#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 https://en.wikipedia.org/wiki/Arithmetic%E2%80%93geometric_mean
    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
# parution http://www.ams.org/notices/201208/rtx120801094p.pdf
# author MAGM http://semjonadlaj.com/
    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()
python matplotlib surface
© www.soinside.com 2019 - 2024. All rights reserved.