You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transform chaining seems to not be possible in some cases. As an example
tr = ax.transAxes - ax.transData + ax.transData
gives a different result than
tr = ax.transAxes
although those are linear transformations, which can easily be inverted, so one would expect
A + B^-1 + B == A
The following example shows that this is not the case.
Code for reproduction
The code should create a red and a green line from [0,0] to [1,1] in axes cooridinates. Yet the red line is somehow fixed to its initial prosition and forgets about its transform.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# apply transform A + B^-1 + B
# this should equal A
tr = ax.transAxes - ax.transData
trinv = ax.transData
line = plt.Line2D([0,1],[0,1],transform=tr+trinv, color="crimson")
ax.add_line(line)
# apply transform A
line2 = plt.Line2D([0,1],[0,1],transform=ax.transAxes, color="limegreen")
ax.add_line(line2)
ax.set_ylim(.1,1.1)
plt.show()
Actual outcome
Expected outcome
A plot with red and green line overlapping.
Now it may well be that I'm completely misunderstanding the transform chaining in which case it would probably be good to update the documentation.
does not work. Seemingly D^-1 + C^-1 is not actually the same as D^-1 - C.
So in total, the actual problem in terms of code is solved, but I'm left with the mysteries:
Why is A + B^-1 not giving the same result as D^-1 + C^-1?
Why is mtransforms.BboxTransformFrom().inverted() not giving the same result as mtransforms.BboxTransformTo()
Why is D^-1 + C^-1 is not actually the same as D^-1 - C (i.e. why is substraction not the same as inversion?)
So apparently all of this is not actually a problem in the real world, since matplotlib works fine as we all know. Still it's somehow strange that some of the transform features are not working as expected.
Note that if you think this is not an issue at all, feel free to close this; but it would be great if someone could shed some light on this. In this case I would also volunteer to update the Transformation tutorial, since there apparently some loopholes to be closed therein.
The problem (originally, I did not look at the other cases) is that .inverted copies the matrix; hence it is not linked with the original transform. So when you change the limits, the forward transform changes, but the inverse does not. If you call ax.set_ylim before adding the lines, then the two overlap. (But if you zoom in or out they don't.)
Yep, that'll be it. The solution from above only works because ax.transScale.inverted() == ax.transScale == Identity in this case.
So let's make this a wishlist proposal to either make the inverted transforms aware of changes or to at least provide updated inverted transforms for the usual cases like ax.transAxesInverted or ax.transDataInverted such that transforming back and forth between the different systems is easily possible.
(I'm aware that this would involve a lot of work, especially distinguishing the cases for which no general inverse exists, so no pressure here. ;-))
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Activity
ImportanceOfBeingErnest commentedon Mar 9, 2018
Also the comments in the source suggest that the above chaining should be possible.
matplotlib/lib/matplotlib/transforms.py
Lines 1381 to 1394 in c85b029
ImportanceOfBeingErnest commentedon Mar 11, 2018
I think I solved this issue as far as to get the correct transform chain, such that the above example works. So let
We want to perform
This does not work as expected, as seen above. If we now define
we can replace B according to
matplotlib/lib/matplotlib/axes/_base.py
Line 668 in 4391fe9
Hence
So we might expect that
would work. But it still doesn't.
However, since
matplotlib/lib/matplotlib/axes/_base.py
Lines 662 to 663 in 4391fe9
we might use
This suddenly works,
produces
Note however that
does not work. Seemingly
D^-1 + C^-1is not actually the same asD^-1 - C.So in total, the actual problem in terms of code is solved, but I'm left with the mysteries:
A + B^-1not giving the same result asD^-1 + C^-1?mtransforms.BboxTransformFrom().inverted()not giving the same result asmtransforms.BboxTransformTo()D^-1 + C^-1is not actually the same asD^-1 - C(i.e. why is substraction not the same as inversion?)So apparently all of this is not actually a problem in the real world, since matplotlib works fine as we all know. Still it's somehow strange that some of the transform features are not working as expected.
Note that if you think this is not an issue at all, feel free to close this; but it would be great if someone could shed some light on this. In this case I would also volunteer to update the Transformation tutorial, since there apparently some loopholes to be closed therein.
QuLogic commentedon Mar 12, 2018
The problem (originally, I did not look at the other cases) is that
.invertedcopies the matrix; hence it is not linked with the original transform. So when you change the limits, the forward transform changes, but the inverse does not. If you callax.set_ylimbefore adding the lines, then the two overlap. (But if you zoom in or out they don't.)ImportanceOfBeingErnest commentedon Mar 24, 2018
Yep, that'll be it. The solution from above only works because
ax.transScale.inverted() == ax.transScale == Identityin this case.So let's make this a wishlist proposal to either make the inverted transforms aware of changes or to at least provide updated inverted transforms for the usual cases like
ax.transAxesInvertedorax.transDataInvertedsuch that transforming back and forth between the different systems is easily possible.(I'm aware that this would involve a lot of work, especially distinguishing the cases for which no general inverse exists, so no pressure here. ;-))
github-actions commentedon May 2, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
tacaswell commentedon Jun 1, 2023
I'm going to re-open this at it is a funny edge case in our transform stack (apparently one we do not use internally).
[-]Transform chaining not possible?[/-][+]inverse transform break chaining[/+]github-actions commentedon Jul 8, 2024
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!