Skip to content

inverse transform break chaining #10741

Open

Description

@ImportanceOfBeingErnest

Bug report

Bug summary

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

image

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.

Matplotlib version

  • Operating system: Windows 8.1
  • Matplotlib version: 2.2
  • Matplotlib backend: Qt4Agg as well as TkAgg
  • Python version: 2.7.10

Activity

  1. ImportanceOfBeingErnest commented on Mar 9, 2018

    @ImportanceOfBeingErnest
    MemberAuthor

    Also the comments in the source suggest that the above chaining should be possible.

    # normally
    A - B == a + b.inverted()
    # sometimes, when A contains the tree B there is no need to
    # descend all the way down to the base of A (via B), instead we
    # can just stop at B.
    (A + B) - (B)^-1 == A
    # similarly, when B contains tree A, we can avoid decending A at
    # all, basically:
    A - (A + B) == ((B + A) - A).inverted() or B^-1
    For clarity, the result of ``(A + B) - B + B == (A + B)``.

  2. ImportanceOfBeingErnest commented on Mar 11, 2018

    @ImportanceOfBeingErnest
    MemberAuthor

    I think I solved this issue as far as to get the correct transform chain, such that the above example works. So let

    A := ax.transAxes
    B := ax.transData
    

    We want to perform

    tr = A + B^-1 
    trinv = B
    transform = tr + trinv
    

    This does not work as expected, as seen above. If we now define

    C := ax.transScale
    D := ax.transLimits
    

    we can replace B according to

    self.transData = self.transScale + (self.transLimits + self.transAxes)

    B = C + (D + A)
    

    Hence

    A = (A + B^-1) + B
      = (A + (C + (D + A))^-1) + B
      = (A + (D + A)^-1 + C^-1) + B
      = A + (A^-1 + D^-1 + C^-1) + B
      = (D^-1 + C^-1) + B
    

    So we might expect that

    tr = ax.transLimits.inverted() + ax.transScale.inverted()
    

    would work. But it still doesn't.

    However, since

    self.transLimits = mtransforms.BboxTransformFrom(
    mtransforms.TransformedBbox(self.viewLim, self.transScale))

    we might use

    ax.transLimits.inverted() == mtransforms.BboxTransformTo(mtransforms.TransformedBbox(ax.viewLim, ax.transScale))
    

    This suddenly works,

    tr =  mtransforms.BboxTransformTo(
            mtransforms.TransformedBbox(ax.viewLim, ax.transScale))  + \
          ax.transScale.inverted()
    trinv = ax.transData 
    line = plt.Line2D([0,1],[0,1],transform=tr+trinv, color="crimson")
    ax.add_line(line)
    

    produces

    image

    Note however that

    tr =  mtransforms.BboxTransformTo(
            mtransforms.TransformedBbox(ax.viewLim, ax.transScale))  - ax.transScale
    

    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:

    1. Why is A + B^-1 not giving the same result as D^-1 + C^-1?
    2. Why is mtransforms.BboxTransformFrom().inverted() not giving the same result as mtransforms.BboxTransformTo()
    3. 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.

  3. QuLogic commented on Mar 12, 2018

    @QuLogic
    Member

    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.)

  4. ImportanceOfBeingErnest commented on Mar 24, 2018

    @ImportanceOfBeingErnest
    MemberAuthor

    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. ;-))

  5. github-actions commented on May 2, 2023

    @github-actions

    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!

  6. added
    status: inactive
    on May 2, 2023
  7. added
    status: closed as inactive
    on Jun 1, 2023
  8. added this to the future releases milestone on Jun 1, 2023
  9. added
    Difficulty: Hard
    and removed
    status: inactive
    status: closed as inactive
    on Jun 1, 2023
  10. tacaswell commented on Jun 1, 2023

    @tacaswell
    Member

    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).

  11. changed the title [-]Transform chaining not possible?[/-] [+]inverse transform break chaining[/+] on Jun 20, 2023
  12. github-actions commented on Jul 8, 2024

    @github-actions

    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!

  13. added
    status: inactive
    on Jul 8, 2024
  14. added
    status: closed as inactive
    on Aug 7, 2024
  15. added
    keep
    and removed
    status: inactive
    status: closed as inactive
    on Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: HardNew featurekeeptopic: transforms and scales

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions