python - 使用仿射变换添加 PatchCollection

标签 python matplotlib affinetransform

我有一些 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)
enter image description here

最佳答案

看起来像 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/

相关文章:

某些分发命令的 python SSL 证书验证失败

python - 由于 AttributeError : module 'PyQt5.QtGui' has no attribute 'QApplication' ,在 PyCharm 中导入 matplotlib.pyplot 失败

Python/Gridspec height_ratio 问题

python - 将鼠标悬停在饼图上时如何显示注释并展开楔形?

python - Matplotlib 填充 alpha 改变颜色

java - 确定 Java 中是否在旋转矩形内单击鼠标(AWT、swing)

Java 仿射变换

python - 非法指令: 4 when importing python pandas

java - 有没有办法缩小 BufferedImage

python - 从图像中获取颜色并表示它