编辑:本文末尾的解决方法。
其中一个例子是:
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
我仍然收到一条错误消息,但至少我得到了一些输出。