I have some patches on which I apply different Affine2D transformations in matplotlib.
Is there a possibility to add them as a collections.PatchCollection? Somehow I am only able to draw them if I call ax.add_patch() separately for each of them.
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)

It looks like
PatchCollectiondoes not support individually transformed elements. From the Matplotlib documentation, we can read aCollectionis aYou can understand this with creating the collection without any individually transformed patches:
That prints
IdentityTransform()for the last statement, and correctly displays the (non-transformed) patches. These patches can be transformed all-at-once from thePatchCollection, without individual specification.On the contrary, when you apply the individual transform for each patch (like in your case), the
.get_transform()method returns an empty list. This is probably due to the fact thatPatchCollectionclasses are made to gatherpatcheswith a lot of common attributes in order to accelerate the drawing efficiency (as mentioned above), including thetransformattribute.Note: on this answer, you can find a workaround with a
patchtopathconversion, then to aPathCollectionwith an increase drawing efficiency compared to individual patch draw.