Skip to content

Transforms Cannot be Added to Subplots in Python3 #8395

Description

@w2naf

Bug report

Bug summary

Axes with transforms (PolarTransform or CompositeGenericTransform) cannot be added to a subplot in python3. This appears to be due to the fact that matplotlib fails to create a hashable version of these objects for the purpose of making a dictionary key in

def _make_key(self, *args, **kwargs):
'make a hashable key out of args and kwargs'
def fixitems(items):
#items may have arrays and lists in them, so convert them
# to tuples for the key
ret = []
for k, v in items:
# some objects can define __getitem__ without being
# iterable and in those cases the conversion to tuples
# will fail. So instead of using the iterable(v) function
# we simply try and convert to a tuple, and proceed if not.
try:
v = tuple(v)
except Exception:
pass
ret.append((k, v))
return tuple(ret)
def fixlist(args):
ret = []
for a in args:
if iterable(a):
a = tuple(a)
ret.append(a)
return tuple(ret)
key = fixlist(args), fixitems(six.iteritems(kwargs))
return key
. There is no bug in the python2 version.

Code for reproduction

  • Minimum working code:
%pylab inline
import matplotlib as mpl
from matplotlib import pyplot as plt

import mpl_toolkits.axisartist.floating_axes as floating_axes

tr_rotate   = mpl.transforms.Affine2D().rotate(3.1459)
tr          = mpl.projections.polar.PolarTransform() +tr_rotate
grid_helper = floating_axes.GridHelperCurveLinear(tr, extremes=(0, 6.28318, 10., 20.))

fig         = plt.figure()
ax1         = floating_axes.FloatingSubplot(fig, 111, grid_helper=grid_helper)

fig.add_subplot(ax1, transform=tr)

Actual outcome

  • Traceback from using python3 running in a Jupyter notebook:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py in add(self, key, a)
    122         try:
--> 123             hash(key)
    124         except TypeError:

TypeError: unhashable type: 'CompositeGenericTransform'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-d333d90be1c9> in <module>()
     11 ax1         = floating_axes.FloatingSubplot(fig, 111, grid_helper=grid_helper)
     12 
---> 13 fig.add_subplot(ax1, transform=tr)

/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py in add_subplot(self, *args, **kwargs)
   1021             a = subplot_class_factory(projection_class)(self, *args, **kwargs)
   1022 
-> 1023         self._axstack.add(key, a)
   1024         self.sca(a)
   1025         a._remove_method = self.__remove_ax

/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py in add(self, key, a)
    123             hash(key)
    124         except TypeError:
--> 125             raise ValueError("first argument, %s, is not a valid key" % key)
    126 
    127         a_existing = self.get(key)

TypeError: not all arguments converted during string formatting

Expected outcome

  • Figure generated by python2 in a Jupyter notebook:

download

Matplotlib version

  • Matplotlib version: 2.0.0

  • Python version: 3.5.2 (bug) / 2.7.12 (working)

  • Platform: Ubuntu 16.04

  • Python was installed using apt.

  • Matplotlib was installed using pip.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions