0

I am trying to derive a class from matplotlib.patches.RegularPolygon. The immediate aim is to have a somewhat unified API for matplotlib.patches.Circle and matplotlib.patches.RegularPolygon (they differ in some of their properties), which I can then use in the rest of my application. As all of the code is written for Circle so far, it makes sense to modify the properties of RegularPolygon.

However, for some reason, arguments are not passed through correctly -- at least that is my best explanation so far. I could use some pointers why that is the case. I have stared at the source code (link) for a long time and don't see it (potentially because I am looking in the wrong place).

MWE

import matplotlib

class RegularPolygon(matplotlib.patches.RegularPolygon):
    def __init__(self, *args, **kwargs):
        super(matplotlib.patches.RegularPolygon, self).__init__(*args, **kwargs)

RegularPolygon((0.,0.), 5, radius=5, orientation=0)

Error message

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-1189a0acbaf3> in <module>()
----> 1 RegularPolygon((0.,0.), 5, radius=5, orientation=0)

<ipython-input-3-3cc2c77e24bf> in __init__(self, *args, **kwargs)
      6
      7     def __init__(self, *args, **kwargs):
----> 8         super(matplotlib.patches.RegularPolygon, self).__init__(*args, **kwargs)

/home/paul/.virtualenvs/base2/local/lib/python2.7/site-packages/matplotlib/patches.pyc in __init__(self, edgecolor, facecolor, color, linewidth, linestyle, antialiased, hatch, fill, capstyle, joinstyle, **kwargs)
     93             self.set_color(color)
     94         else:
---> 95             self.set_edgecolor(edgecolor)
     96             self.set_facecolor(facecolor)
     97         # unscaled dashes.  Needed to scale dash patterns by lw

/home/paul/.virtualenvs/base2/local/lib/python2.7/site-packages/matplotlib/patches.pyc in set_edgecolor(self, color)
    282         """
    283         self._original_edgecolor = color
--> 284         self._set_edgecolor(color)
    285
    286     def set_ec(self, color):

/home/paul/.virtualenvs/base2/local/lib/python2.7/site-packages/matplotlib/patches.pyc in _set_edgecolor(self, color)
    270                 set_hatch_color = False
    271
--> 272         self._edgecolor = colors.to_rgba(color, self._alpha)
    273         if set_hatch_color:
    274             self._hatch_color = self._edgecolor

/home/paul/.virtualenvs/base2/local/lib/python2.7/site-packages/matplotlib/colors.pyc in to_rgba(c, alpha)
    132         rgba = _colors_full_map.cache[c, alpha]
    133     except (KeyError, TypeError):  # Not in cache, or unhashable.
--> 134         rgba = _to_rgba_no_colorcycle(c, alpha)
    135         try:
    136             _colors_full_map.cache[c, alpha] = rgba

/home/paul/.virtualenvs/base2/local/lib/python2.7/site-packages/matplotlib/colors.pyc in _to_rgba_no_colorcycle(c, alpha)
    187     c = tuple(c.astype(float))
    188     if len(c) not in [3, 4]:
--> 189         raise ValueError("RGBA sequence should have length 3 or 4")
    190     if len(c) == 3 and alpha is None:
    191         alpha = 1

ValueError: RGBA sequence should have length 3 or 4
Paul Brodersen
  • 11,221
  • 21
  • 38

1 Answers1

1

Using

super(matplotlib.patches.RegularPolygon, self).__init__()

you calling the init function of the parent of matplotlib.patches.RegularPolygon. However you would really need to call the init of matplotlib.patches.RegularPolygon itself.

I would also suggest not to use the same name for the subclassed artist, as this might add to confusion here.

Options you have

  • Old style

    import matplotlib
    
    class MyRegularPolygon(matplotlib.patches.RegularPolygon):
        def __init__(self, *args, **kwargs):
            matplotlib.patches.RegularPolygon.__init__(self, *args, **kwargs)
    
    r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)
    
  • New Style (py2 & 3)

    import matplotlib
    
    class MyRegularPolygon(matplotlib.patches.RegularPolygon):
        def __init__(self, *args, **kwargs):
            super(MyRegularPolygon, self).__init__(*args, **kwargs)
    
    r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)
    
  • New Style (only py3)

    import matplotlib
    
    class MyRegularPolygon(matplotlib.patches.RegularPolygon):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
    r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)
    

I would suggest reading What does 'super' do in Python? for some explanation of super.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712