3d p分段函数

2024-06-16 15:17:12 发布

您现在位置:Python中文网/ 问答频道 /正文

由于广播错误,我无法使np.piecewise用于多维绘图。在

有人有办法避开这个问题吗?在

以下是我在一个简化的可执行脚本中的内容:

import numpy as np
from pylab import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D 

num_steps = 100
x_arr = np.linspace(0,100, num_steps)
y_arr = np.linspace(0,20, num_steps)

def zfunc(x, y):
    return np.piecewise(x, [x>=500, x<500], [x, -x])

x,y = np.meshgrid(x_arr, y_arr)
z =zfunc(x,y)

fig=plt.figure()
ax=fig.subplot(1,1,1,projection='3d')
p = x.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.coolwarm,linewidth=0,antialiased=False)
plt.show()

从而得出错误:

^{pr2}$

Tags: fromimport绘图as错误npfigplt
1条回答
网友
1楼 · 发布于 2024-06-16 15:17:12

查看您使用的函数的docstring通常是一个好主意。我在那里找到了解决办法。在

np.piecewise(x, [x>=500, x<500], [lambda x: x, lambda x: -x])

funclist : list of callables, f(x,args,*kw), or scalars Each function is evaluated over x wherever its corresponding condition is True. It should take an array as input and give an array or a scalar value as output. If, instead of a callable, a scalar is provided then a constant function (lambda x: scalar) is assumed.

相关问题 更多 >