【问题标题】:Inheriting from `matplotlib.patches.RegularPolygon` yields `ValueError` on instantiation从 `matplotlib.patches.RegularPolygon` 继承在实例化时会产生 `ValueError`
【发布时间】:2018-06-18 13:02:31
【问题描述】:

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

但是,由于某种原因,参数没有正确传递——至少这是我迄今为止最好的解释。我可以使用一些指针为什么会这样。盯着源码(link)看了很久,没看到(可能是找错地方了)。

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)

错误信息

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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    使用

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

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

    我还建议不要为子类艺术家使用相同的名称,因为这可能会在这里增加混乱。

    你有的选择

    • 旧式

      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)

      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)

      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)
      

    我建议阅读What does 'super' do in Python? 以了解super 的一些解释。

    【讨论】:

    • 谢谢欧内斯特。正如我所怀疑的那样,我很密集。平常的一天,我知道super 的工作原理,我保证!
    • * 在正常的一天。这不是。很明显。
    • 不要忘记许多可能的调试步骤之一是“呼吸新鲜空气”或“明天再看一遍”。 :-)
    猜你喜欢
    • 2011-01-17
    • 2021-08-24
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多