gpt4 book ai didi

matplotlib 在 PatchCollection 中更改一个 Patch

转载 作者:行者123 更新时间:2023-12-02 02:15:30 31 4
gpt4 key购买 nike

PatchCollection 接受一个 Patches 列表,并允许我一次将它们全部转换/添加到 Canvas 。但是在构建 PatchCollection 对象之后对其中一个 Patch 的更改不会反射(reflect)出来

例如:

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 集合,它将补丁分组,以便我可以将它们一起转换,但我也希望能够更改单个补丁。

最佳答案

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

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 中更改一个 Patch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903554/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com