3

PatchCollection接受Patches 列表并允许我一次将它们转换/添加到画布中。但是构造对象Patch后对es之一的更改并没有体现出来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 集合,它将对补丁进行分组,以便我可以将它们一起转换,但我也希望能够更改单个补丁。

4

1 回答 1

3

我不知道有一个集合可以满足您的需求,但是您可以很容易地为自己编写一个:

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)
于 2012-06-14T21:12:23.727 回答