在3D中绘制分段函数

时间:2016-03-16 13:51:04

标签: python numpy matplotlib plot

我是python的新手并试图用3d绘制分段函数。我正试图用3d绘制“主要公式”。 z轴上的函数随着x和y的变化而变化,范围从0到10,常数= 1.但我似乎无法在这里找出绘图方法。

from sympy import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

def mainformula(x,y,constant):
    return Piecewise((subformula1(x,y,constant), y >= 0 and y < 3),(subformula2(x,y,constant),y>=3 and y <= 10))

def subformula1(x,y,constant):
    return x + y + constant

def subformula2(x,y,constant):       
    return x - y - constant

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 10, 0.25)
Y = np.arange(0, 10, 0.25)
constant = 1
X, Y = np.meshgrid(X, Y)
Z = mainformula(X,Y,constant)
surf = ax.plot_surface(X, Y, Z)
plt.show()

运行该代码时得到的错误是:&#34; ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()&#34;

2 个答案:

答案 0 :(得分:1)

您正在处理数组,因此它永远无法在布尔上下文中使用array > 3(例如使用and)这将始终为您提供所收到的错误。但是,您始终可以将条件定义为布尔掩码,并在适当的元素上运行公式:

def mainformula(x,y,constant):
    z = np.zeros_like(x)
    # Condition 1 indexes all elements where subformula 1 is valid
    condition1 = np.logical_and(y >= 0, y < 3)
    # condition1 = (y >= 0) & (y < 3) # is another way of writing it
    z[condition1] = x[condition1] + y[condition1] + constant
    # now do it in the range where subformula 2 is valid
    condition2 = np.logical_and(y >= 3, y <= 10)
    # condition1 = (y >= 3) & (y <= 10) # is another way of writing it
    z[condition2] = x[condition2] - y[condition2] - constant

    return z

这不会使用sympy.Piecewise,但只在尝试绘图时才能正常工作。如果您需要单独的函数而不是在主公式中完成所有操作,则需要稍微更改一下:

z[condition1] = subformula1(x[condition1], y[condition1], constant)

condition2类似。

答案 1 :(得分:0)

问题是你试图将逻辑断言变成没有直接断言的数组。 Python很难断言这是什么:

    y >= 0 and y < 3

因此,您必须将代码更改为可以理解的内容:

    def mainformula(x,y,constant):
        y2 = y[y>=0]
        y2 = y2[y2<3]
        y3 = y[y>=3]
        y3 = y2[y2<=10]
        return Piecewise((subformula1(x,y,constant), y2),(subformula2(x,y,constant),y3))

问题是Piecewise函数似乎也没有接受一些数组到你拥有的参数之一。您可能需要重新考虑您的问题,可能是通过为Piecewise函数构建一个循环,以便在每个元素上启动。

相关问题