Python 2.7 尝试在3D matplotlib中绘制多元函数;返回空数字

Python 2.7 尝试在3D matplotlib中绘制多元函数;返回空数字,python-2.7,matplotlib,mplot3d,Python 2.7,Matplotlib,Mplot3d,我正试图在3D matplotlib中绘制函数F(x1,x2),遵循以下教程: 一旦我试着运行代码,结果显示图是空的,甚至看不到轴输出。我想知道是否有人能找出这种行为背后的原因。我正在使用python 2.7 from __future__ import division from numpy import exp,arange from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show impor

我正试图在3D matplotlib中绘制函数F(x1,x2),遵循以下教程:

一旦我试着运行代码,结果显示图是空的,甚至看不到轴输出。我想知道是否有人能找出这种行为背后的原因。我正在使用python 2.7

from __future__ import division

from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
import math 
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
from matplotlib import pylab
from numpy import arange,array,ones
from scipy import stats
import numpy
import matplotlib.ticker as mtick
import sys
import os


# the function that I'm going to plot
def z_func(x1,x2):
 return exp(-(1-x1)**2 - 100*((x2-x1**2)**2))


x1 = arange(5.0,-5.0,-0.01)
x2 = arange(-5.0,5.0,0.01)
X1,X2 = meshgrid(x1, x2) # grid of point
Z = z_func(X1, X2) # evaluation of the function on the grid


fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X1, X2, Z, rstride=1, cstride=1, cmap=cm.RdBu,linewidth=0, antialiased=False)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
ax.view_init(elev=25, azim=-120)

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

你的代码对我有用。我只需要等到计算机完成计算。计算时间长是因为
x1
x2
的大小。尝试更改以下行:

x1 = arange(5.0,-5.0,-0.01)
x2 = arange(-5.0,5.0,0.01)
以下几行:

x1 = arange(5.0,-5.0,-0.1)
x2 = arange(-5.0,5.0,0.1)
p、 我建议你们安排进口。您只需要以下内容:

from numpy import exp, arange
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from pylab import meshgrid
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm