如何在python中创建Schwefel 2.21 3D图?

时间:2019-07-20 01:28:23

标签: python matplotlib

如何在python中创建here的Schwefel 2.21函数3D图。

我尝试过Z = np.abs(X) + np.abs(Y);但是,它不起作用,与Z = np.max(np.abs(X))+ np.max(np.abs(Y))

相同
def makePlot(name, X, Y, Z, lb, ub):

    fig = plt.figure(name)
    ax = plt.axes(projection='3d')
    ax.set_xlabel("x1"); ax.set_ylabel("x2"); ax.set_zlabel("f(x1,x2)")

    surf = ax.plot_surface(X, Y, Z, cmap='jet', rstride=1, cstride=1, linewidth=0)

    #fig.colorbar(surf, shrink=0.5, aspect=5)


    #ax.contourf(X, Y, Z, zdir='z', offset=0, cmap='jet', alpha = 0.5)

    ax.set_yticks(np.arange(lb, ub+1, ub // 2))
    ax.set_xticks(np.arange(lb, ub+1, ub // 2))

    # make the panes transparent
    ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
    ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
    ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))


    #fig.set_size_inches(width, height)
    fig.savefig(name+ '.png')
    fig.savefig(name+ '.pdf')

    plt.show()


def xyAxis(lb, ub):

    step = 0.1

    X = np.arange(lb, ub, step)
    Y = np.arange(lb, ub, step)
    X, Y = np.meshgrid(X, Y)

    return X,Y

def Schwefel221(lb, ub):

    name = 'Schwefel221'

    X, Y = xyAxis(lb,ub)

    Z = np.abs(X) + np.abs(Y)

    makePlot(name, X, Y, Z, lb, ub)

Schwefel221(-10,10)

0 个答案:

没有答案