ValueError:数组的真值是模糊的

时间:2017-09-13 18:13:29

标签: arrays python-3.x numpy

代码:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D

    r = np.linspace(0.02, 0.98, 10)
    theta = np.linspace(0, 2*np.pi)
    rgrid, thetagrid = np.meshgrid(r, theta)

    xgrid, ygrid = rgrid*np.cos(thetagrid), rgrid*np.sin(thetagrid)

    def get_zvalue(r):
        if r<0.03:
            return 0
        elif r>0.02:
            delta_s = 0.02/np.sqrt(1-(r-0.02)**2)
            return delta_s + get_zvalue(r-0.02)

    zsurface = get_zvalue(r)



    fig = plt.figure()
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(xgrid, ygrid, zsurface)
    plt.savefig("Hoops.png")

错误:

    ValueError: The truth value of an array with more than one element is 
    ambiguous. Use a.any() or a.all()

ValueError由函数get_zvalue中的行“if r&lt; 0.03:”触发。我没有用numpy做太多,也不知道该怎么做这个错误。有没有其他方法来创建zsurface数组而不会收到错误?

0 个答案:

没有答案