Error: _string_to_bool in mplot3d (workaround found)

Hennep picture Hennep · Aug 19, 2014 · Viewed 7.5k times · Source

Edit: workaround at the end of this post.

I was trying to run some of the examples provided here and here.

One of these examples was:

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()

This gave me the following error:

/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'

The error is originated from: ax = fig.add_subplot(111, projection='3d')

I tried to check and upgrade matplotlib. Running python -c 'import matplotlib; print matplotlib.__version__' gave me 1.4.x.

I dived in the underlying code and found this:

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)

Can anyone give me a suggestion where to go further?

Edit: I have found a workaround for this problem. As can be seen from the last error message something went wrong in the _string_to_bool function. Simply add the following line

from matplotlib.cbook import _string_to_bool

on top of

/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py 

I still get an error message, but at least I get some output.

Answer

tsbertalan picture tsbertalan · May 14, 2015

For me, (Ubuntu 14.04, using the system Python) the problem was solved by removing the apt package python-matplotlib (prompted by this bug report). A newer matplotlib was also installed in /usr by pip; I think some newer pip packages were somehow getting modules from the older apt packages.

This was after trying meyerson's pip install --upgrade --ignore-installed matplotlib[mplot3d] command, which did result in NumPy being recompiled (and, I suppose, Matplotlib being reinstalled), but didn't solve the problem.

It's also interesting to note that I only had the problem when running the script in question (a batch of unittests) at the command line, but not when running in the LiClipse test runner (which uses a different, explicitly changeable, PYTHONPATH order. I regret I did not try changing this order to see if I could reproduce the problem in Eclipse.