本文介绍了matplotlib更改PatchCollection中的补丁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PatchCollection
接受Patch
es的列表,并允许我一次将它们转换/添加到画布.但是在构建PatchCollection
对象之后对Patch
es之一的更改不会反映出来
PatchCollection
accepts a list of Patch
es and allows me to transform / add them to a canvas all at once. But changes to the one of the Patch
es after the construction of the PatchCollection
object are not reflected
例如:
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集合来将补丁分组,以便可以将它们一起转换,但是我也希望能够更改单个补丁.
I'm looking for a matplotlib collection that will group patches just so I can transform them together, but I want to be able to change the individual patches as well.
推荐答案
我不知道可以满足您需要的集合,但是您可以很轻松地为自己编写一个:
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)
这篇关于matplotlib更改PatchCollection中的补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!