Skip to content Skip to sidebar Skip to footer

Inheriting From `matplotlib.patches.regularpolygon` Yields `valueerror` On Instantiation

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.Regul

Solution 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
    
    classMyRegularPolygon(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
    
    classMyRegularPolygon(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
    
    classMyRegularPolygon(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.

Post a Comment for "Inheriting From `matplotlib.patches.regularpolygon` Yields `valueerror` On Instantiation"