Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 替代matplotlib.artist.artist以绘制复杂形状_Python_Matplotlib - Fatal编程技术网

Python 替代matplotlib.artist.artist以绘制复杂形状

Python 替代matplotlib.artist.artist以绘制复杂形状,python,matplotlib,Python,Matplotlib,我想在matplotlib中创建一个艺术家,它可以绘制一个包含封装在FancyBoxPatch中的文本和图像的复合形状。我从前面提到的FancyBoxPatch派生了一个类,并重写了“draw”方法,但它似乎不起作用 我试图实现的是一个对象,它可以由matplotlib绘制,但比可用的简单补丁更复杂;有点像GUI设计中的复合小部件的概念 以下是我尝试过的: class Cell(FancyBboxPatch): def __init__(self, xy, width, height,

我想在matplotlib中创建一个艺术家,它可以绘制一个包含封装在FancyBoxPatch中的文本和图像的
复合
形状。我从前面提到的FancyBoxPatch派生了一个类,并重写了“draw”方法,但它似乎不起作用

我试图实现的是一个对象,它可以由matplotlib绘制,但比可用的简单补丁更复杂;有点像GUI设计中的复合小部件的概念

以下是我尝试过的:

class Cell(FancyBboxPatch):

    def __init__(self, xy, width, height, **kwargs):
        FancyBboxPatch.__init__(self, xy, width, height, **kwargs)

    def draw(self, renderer):
        print "Overridden draw method"
        FancyBboxPatch.draw(self, renderer)

        # Try drawing some simple patches and text:
        r = Rectangle((set._x, self._y), self._width, self._height)
        r.draw(renderer) # this doesn't draw

        t = Annotation("hi", (self._x, self._y))
        t.draw(renderer) # this causes an error
但这是行不通的。未绘制矩形,注释引发错误:
AttributeError:'NoneType'对象没有属性“transData”

我觉得我走错了路!我可以用这种方式覆盖
draw
方法吗

TIA

试试这个:

  • 首先,您应该尝试将*args添加到init参数中,并将其传递到FancyBoxPatch中。init
  • 当重写draw时,您应该在完成更改后(或之前)调用原始draw,因为它可能会调用一些您不知道的内部方法,请检查FancyBoxPatch.draw的源代码

正如我所看到的,FancyBoxPatch是Patch的一个子类,Patch是Artister的子类,Patch和Artister都有方法draw,所以你不能不调用原始方法就简单地覆盖它们


编辑:我是超级盲,很抱歉,您正在调用draw方法,但是最好将*args和**kwargs添加到任何重写的方法中。。试试看,也许在重写方法的末尾调用FancyBoxPatch.draw

重写和时,语法会有所不同。它可能会帮助你解决这个问题。因此,对重写的“draw”方法的调用应该是:

super(Cell,self).draw(renderer)
您的构造函数也是如此:

super(Cell,self).__init__(xy, width, height, **kwargs)

添加*args并将draw方法移动到覆盖的draw的末尾没有任何区别……我这样做对吗?在Android/Gtk/Qt等GUI环境中执行类似操作时,我就是这样处理问题的。也许这不是matplotlib的正确方法?我是不是心态不对?!