我有一些 patches
我应用了不同的Affine2D
matplotlib
中的转换.
是否有可能将它们添加为 collections.PatchCollection
?不知何故,我只能在调用 ax.add_patch()
时才能绘制它们。分别为他们每个人。
from matplotlib import pyplot as plt, patches, collections, transforms
fig, ax = plt.subplots()
trafo1 = transforms.Affine2D().translate(+0.3, -0.3).rotate_deg_around(0, 0, 45) + ax.transData
trafo2 = transforms.Affine2D().translate(+0.3, -0.3).rotate_deg_around(0, 0, 65) + ax.transData
rec1 = patches.Rectangle(xy=(0.1, 0.1), width=0.2, height=0.3, transform=trafo1, color='blue')
rec2 = patches.Rectangle(xy=(0.2, 0.2), width=0.3, height=0.2, transform=trafo2, color='green')
ax.add_collection(collections.PatchCollection([rec1, rec2], color='red', zorder=10))
# ax.add_patch(rec1)
# ax.add_patch(rec2)
最佳答案
看起来像 PatchCollection
不支持单独转换的元素。来自 Matplotlib documentation , 我们可以读到 Collection
是一个
class for the efficient drawing of large collections of objects that share most properties, e.g., a large number of line segments or polygons.
您可以通过创建没有任何单独转换补丁的集合来理解这一点:
rec1 = patches.Rectangle(xy=(0.1, 0.1), width=0.2, height=0.3, color='blue')
rec2 = patches.Rectangle(xy=(0.2, 0.2), width=0.3, height=0.2, color='green')
col = collections.PatchCollection([rec1, rec2], color='red', zorder=10)
print(col.get_transform())
打印 IdentityTransform()
最后一条语句,并正确显示(未转换的)补丁。这些补丁可以从 PatchCollection
一次性转换。 , 没有单独的规范。相反,当您为每个补丁应用单独的变换时(如您的情况),
.get_transform()
方法返回一个空列表。这可能是因为 PatchCollection
上课是为了收集 patches
有很多共同的属性以加快绘制效率(如上所述),包括transform
属性。备注 : 在 this answer ,您可以使用
patch
找到解决方法至path
转换,然后转换为 PathCollection
与单独的补丁绘制相比,绘制效率有所提高。
关于python - 使用仿射变换添加 PatchCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64083563/