matplotlib change a Patch in PatchCollection

后端 未结 1 1308
醉话见心
醉话见心 2020-12-11 09:42

PatchCollection accepts a list of Patches and allows me to transform / add them to a canvas all at once. But changes to the one of the Patch<

相关标签:
1条回答
  • 2020-12-11 09:50

    I don't know of a collection which will do what you want, but you could write one for yourself fairly easily:

    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)
    
    0 讨论(0)
提交回复
热议问题