简体   繁体   English

当我使用mplot3D时,为什么会出现错误:NameError:未定义名称'plot_trisurf'

[英]When I use mplot3D, why get error: NameError: name 'plot_trisurf' is not defined

I try to use mplot3D to draw some 3D pictures: 我尝试使用mplot3D绘制一些3D图片:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
...
plot_trisurf(X, Y, Z1)

Then my computer will tell me that 然后我的电脑会告诉我

NameError: name 'plot_trisurf' is not defined

Why this happen? 为什么会这样? I thought I have imported it already. 我以为我已经导入了。

plot_trisurf is a method from the Axes3D class. plot_trisurfAxes3D类中的方法。 In python, in order to call a class method, you usually need to provide the class or an instance of it. 在python中,为了调用类方法,通常需要提供类或它的实例。

In this case the class instance would be your axes object. 在这种情况下,类实例将是您的axes对象。 This is usually called ax . 通常称为ax The correct call would therefore be (as seen on the matplotlib trisurf example ): 因此,正确的调用将是(如在matplotlib trisurf示例上所示 ):

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

fig = plt.figure()
ax = fig.gca(projection='3d')
...
ax.plot_trisurf(x, y, z)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM