从`matplotlib.patches.RegularPolygon`继承在实例化时产生`ValueError` [英] Inheriting from `matplotlib.patches.RegularPolygon` yields `ValueError` on instantiation

查看:47
本文介绍了从`matplotlib.patches.RegularPolygon`继承在实例化时产生`ValueError`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 matplotlib.patches.RegularPolygon 派生一个类.直接目标是为 matplotlib.patches.Circlematplotlib.patches.RegularPolygon(它们的某些属性不同)有一个统一的 API,然后我可以在我的应用程序的其余部分使用.到目前为止,由于所有代码都是为 Circle 编写的,因此可以修改 RegularPolygon 的属性.

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

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)

错误信息

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

推荐答案

使用

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

您调用 matplotlib.patches.RegularPolygon 的父级的init函数.但是,您确实需要调用 matplotlib.patches.RegularPolygon 本身的初始化.

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.

您拥有的选项

  • 旧样式

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

  • 新样式(py2 和 3)

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

  • 新样式(仅 py3)

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

  • 我建议阅读 Python 中的super"有什么作用?有关 super 的一些解释.

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

    这篇关于从`matplotlib.patches.RegularPolygon`继承在实例化时产生`ValueError`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆