如何在3D Python图中查找所有局部最小值?

时间:2018-12-09 23:00:43

标签: python plot

我想展示3D非凸图的局部最优值。这是可以显示图的代码,但不能显示局部和全局最优:

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

#Input data is something like this[x,y,z]:
TT = [[2,1,450], [3,1,678],[0,0,890]

def fun(x, y):
    for r in range(len(TT)):
        if x == TT[r][0] and y == TT[r][1]:
            return TT[r][2]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-2.0, 3.0, 1.0)
X, Y = np.meshgrid(x, y)
zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)

ax.plot_surface(X, Y, Z ,cmap=cm.coolwarm,)

plt.show()

0 个答案:

没有答案