2013-02-27 99 views
0

我想要在matplotlib中創建一個藝術家,它可以繪製包含封裝在FancyBboxPatch中的文本和圖像的compound形狀。我從前面提到的FancyBboxPatch派生了一個類,並重寫了「draw」方法,但它似乎不起作用。覆蓋matplotlib.artist.Artist繪製複雜形狀

我想要實現的是一個可由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 

但這不起作用。這個矩形沒有被繪製,Annotation會拋出一個錯誤:AttributeError: 'NoneType' object has no attribute 'transData'

我感覺我會這樣做的錯誤!我可以用這種方法覆蓋draw方法嗎?

TIA

+0

上午我甚至會對此正確的方法是什麼?這就是我在像Android/Gtk/Qt等GUI界面中做類似的事情時會遇到的問題。也許這不是matplotlib的正確方法?我錯了嗎?! – ccbunney 2013-02-27 15:02:50

回答

0

試試這個:

  • 首先,你應該儘量* ARGS加入到INIT PARAMS,也將它傳遞到FancyBboxPatch。 init
  • 覆蓋繪製時,您應該調用原始繪製,之後(或befre)您已完成所做的更改,因爲可能會調用一些您不知道的內部方法,請檢查FancyBboxPatch的源代碼.draw

http://matplotlib.sourcearchive.com/documentation/0.99.3-1/patches_8py-source.html

正如我所看到的,FancyBboxPatch是補丁的一個子類,這是藝術家的子類,這兩個補丁和藝術家有方法得出,所以你不能只是簡單的覆蓋它們,而不調用原方法

編輯:我超級失明,對不起,你打電話的繪製方法,但它是一個好主意,添加*參數和** kwargs任何重寫方法..嘗試,並且可能調用FancyBboxPatch.draw覆蓋方法結束時

+0

添加* args並將繪製方法移到重寫的繪製的結尾沒有區別... – ccbunney 2013-02-27 14:57:39

0

當您覆蓋class methodsinstance methods時,在語法上存在差異。它可以幫助你解決問題。因此,呼籲重寫「畫」的方法應該是:

super(Cell,self).draw(renderer) 

這同樣適用於你的構造:

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