编辑:此帖末尾提供解决方法。
其中一个示例是:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
plt.show()
这给了我以下错误:
/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py:842: MatplotlibDeprecationWarning: The set_scale function was deprecated in version 1.3.
self.zaxis.set_scale('linear')
Traceback (most recent call last):
File "/mnt/hgfs/MCLS/postprocessing/surface3d_demo2.py", line 6, in <module>
ax = fig.add_subplot(111, projection='3d')
File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 958, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-x86_64.egg/matplotlib/axes/_subplots.py", line 78, in __init__
self._axes_class.__init__(self, fig, self.figbox, **kwargs)
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 78, in __init__
*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-x86_64.egg/matplotlib/axes/_base.py", line 436, in __init__
self.cla()
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 847, in cla
Axes.cla(self)
File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-x86_64.egg/matplotlib/axes/_base.py", line 897, in cla
self.grid(self._gridOn, which=rcParams['axes.grid.which'])
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 1057, in grid
self._draw_grid = maxes._string_to_bool(b)
AttributeError: 'module' object has no attribute '_string_to_bool'
错误产生于: ax = fig.add_subplot(111, projection='3d')
我尝试检查和升级matplotlib。运行python -c 'import matplotlib; print matplotlib.__version__'
的结果是1.4.x
。
我深入研究了底层代码,发现了这个问题:
def grid(self, b=True, **kwargs):
'''
Set / unset 3D grid.
.. note::
Currently, this function does not behave the same as
:meth:`matplotlib.axes.Axes.grid`, but it is intended to
eventually support that behavior.
.. versionchanged :: 1.1.0
This function was changed, but not tested. Please report any bugs.
'''
# TODO: Operate on each axes separately
if len(kwargs) :
b = True
self._draw_grid = maxes._string_to_bool(b)
有人可以给我一个建议去哪里继续吗?
编辑:
我已经找到了解决这个问题的方法。正如最后一个错误信息所示,_string_to_bool
函数出了一些问题。只需要添加以下一行即可:
from matplotlib.cbook import _string_to_bool
在...之上
/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py
我仍然收到错误信息,但至少我能得到一些输出。
axes3d.py
中使用该函数的唯一位置,且最后一次修改是在2013年。我怀疑实际问题是您安装的出了问题。 - tacaswellsudo apt-get install python-mpltoolkits.basemap
重新安装了mpl_toolkit。在使用sudo apt-get purge --auto-remove python-mpltoolkits.basemap
并手动删除mpl_toolkits文件夹之后,这个新的安装没有出现任何问题。感谢你的帮助! - Hennep