代码之家  ›  专栏  ›  技术社区  ›  Paul Brodersen

从'matplotlib.patches.regularPolygon'继承在实例化时生成'valueerror'

  •  0
  • Paul Brodersen  · 技术社区  · 6 年前

    matplotlib.patches.RegularPolygon . matplotlib.patches.Circle 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
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   ImportanceOfBeingErnest    6 年前

    使用

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

    matplotlib.patches.RegularPolygon matplotlib.patches.RegularPolygon

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

    • 旧款

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