Hen*_*nep 17 matplotlib python-2.7 mplot3d
编辑:本文末尾的解决方法.
其中一个例子是:
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()
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
/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'
Run Code Online (Sandbox Code Playgroud)
错误源自: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)
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我一个建议去哪里更进一步?
编辑:我找到了解决此问题的方法.从上一个错误消息可以看出_string_to_bool函数中出现了问题.只需添加以下行
from matplotlib.cbook import _string_to_bool
Run Code Online (Sandbox Code Playgroud)
在之上
/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py
Run Code Online (Sandbox Code Playgroud)
我仍然收到错误消息,但至少我得到了一些输出.
tsb*_*lan 11
对我来说,(Ubuntu 14.04,使用系统Python)通过删除apt包解决了问题python-matplotlib(由此错误报告提示).一个较新的matplotlib也被pip安装在/ usr中; 我认为一些较新的pip包以某种方式从旧的apt包中获取模块.
这是在尝试meyerson的pip install --upgrade --ignore-installed matplotlib[mplot3d]命令之后,这确实导致NumPy被重新编译(并且,我想,重新安装了Matplotlib),但没有解决问题.
还有一点值得注意的是,我在命令行运行有问题的脚本(一批单元测试)时遇到了问题,但在LiClipse测试运行器(使用不同的,明确可更改的PYTHONPATH命令)中运行时却没有.遗憾我没有尝试更改此顺序以查看是否可以在Eclipse中重现该问题.
我有确切的问题,这是我如何解决它(在我的情况下):
1)重命名(或删除)文件夹mplot3d(因此matplotlib认为它不在那里):
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/mpl_toolkits/mplot3d-old
Run Code Online (Sandbox Code Playgroud)
2)matplotlib使用指定的mplot3d进行更新:
pip install --upgrade matplotlib[mplot3d]
Run Code Online (Sandbox Code Playgroud)
添加到@Hennep的解决方法:
将以下行添加到文件中
/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py
from matplotlib.cbook import _string_to_bool
然后将代码更改为
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 = _string_to_bool(b)
Run Code Online (Sandbox Code Playgroud)
maxes从函数中删除这个词,如果我这样做,一切都会正常工作。