我正在尝试从matplotlib.patches.RegularPolygon
派生一个班级。
目前的目标是为matplotlib.patches.Circle
和matplotlib.patches.RegularPolygon
设置一个统一的API(它们的某些属性不同),然后我可以在我的应用程序的其余部分中使用它。由于到目前为止所有代码都是为Circle
编写的,因此修改RegularPolygon
的属性是有意义的。
但是,出于某种原因,参数没有正确传递 - 至少到目前为止这是我最好的解释。我可以使用一些指针为什么会这样。我已经盯着源代码(link)了很长时间而没有看到它(可能是因为我看错了地方)。
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
答案 0 :(得分: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&amp; 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
的一些解释。