2012-06-05 99 views
2

PatchCollection接受Patch es的列表,並允許我一次將它們轉換/添加到畫布。但更改PatchCollection對象的構造後Patch ES的一個不反映matplotlib更改PatchCollection中的修補程序

例如:

import matplotlib.pyplot as plt 
import matplotlib as mpl 

rect = mpl.patches.Rectangle((0,0),1,1) 

rect.set_xy((1,1)) 
collection = mpl.collections.PatchCollection([rect]) 
rect.set_xy((2,2)) 

ax = plt.figure(None).gca() 
ax.set_xlim(0,5) 
ax.set_ylim(0,5) 
ax.add_artist(collection) 
plt.show() #shows a rectangle at (1,1), not (2,2) 

我正在尋找一個matplotlib集合,將組補丁,這樣我可以改變他們在一起,但我希望能夠改變個人補丁。

回答

2

我不知道一個集合,將你想要做什麼的,但你可以寫一個自己相當容易:

import matplotlib.collections as mcollections 

import matplotlib.pyplot as plt 
import matplotlib as mpl 


class UpdatablePatchCollection(mcollections.PatchCollection): 
    def __init__(self, patches, *args, **kwargs): 
     self.patches = patches 
     mcollections.PatchCollection.__init__(self, patches, *args, **kwargs) 

    def get_paths(self): 
     self.set_paths(self.patches) 
     return self._paths 


rect = mpl.patches.Rectangle((0,0),1,1) 

rect.set_xy((1,1)) 
collection = UpdatablePatchCollection([rect]) 
rect.set_xy((2,2)) 

ax = plt.figure(None).gca() 
ax.set_xlim(0,5) 
ax.set_ylim(0,5) 
ax.add_artist(collection) 
plt.show() # now shows a rectangle at (2,2) 
相關問題