Skip to content

regression for 3.4.x - ax.figbox replacement incompatible to all version including 3.3.4 #19982

Closed

Description

@2sn

Bug report

Bug summary

The replacement of ax.figbox as well as suggested replacement code by deprecation warning produce different outputs than what this did for a decade.

Code for reproduction

(part of plotting package)

Actual outcome

Example form 3.4.1

(Pdb) p ax.figbox
Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]])
(Pdb) ax.get_subplotspec().get_position(ax.figure)
Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]])

Expected outcome

Example from 3.3.4

(Pdb) p ax.figbox
Bbox([[0.07234432444852941, 0.07502374387254904], [0.9598087086397059, 0.9739713541666667]])
(Pdb) ax.get_subplotspec().get_position(ax.figure)
Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]])

Obviously, wrong layout messes up output.

Can a function be added that provides the old figbox information?

(Why was it removed in the first place?)

Matplotlib version

  • Operating system:
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.1
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.9.4
  • Jupyter version (if applicable): 7.22.0
  • Other libraries: N/A

pip3

Activity

  1. 2sn commented on Apr 16, 2021

    @2sn
    Author

    related to #19938

  2. 2sn commented on Apr 16, 2021

    @2sn
    Author

    OK, it should be possible to do this, what I found is (for MPL v. 3.3.4 behaviour)

    In [66]: ax.figbox
    Out[66]: Bbox([[0.07234432444852941, 0.07502374387254904], [0.9598087086397059, 0.9739713541666667]])
    
    In [67]: BboxTransformFrom(ax.figure.bbox).transform_bbox(ax.bbox)
    Out[67]: Bbox([[0.07234432444852941, 0.07502374387254904], [0.9598087086397059, 0.9739713541666668]])
    

    But why not just leave this as a figbox property for ax?

    And why not just with the same result as before?

    (the current replacement, whereas with a deprecation warning, channing its output values still is a bug)

  3. changed the title [-]regression for 3.4.x - ax.figbox replacement incompatible to all version until 3.3.4[/-] [+]regression for 3.4.x - ax.figbox replacement incompatible to all version including 3.3.4[/+] on Apr 16, 2021
  4. anntzer commented on Apr 16, 2021

    @anntzer
    Contributor

    FWIW I cannot repro the difference, I get Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]]) in all cases (code that you run before querying figbox may possibly change the situation, but I cannot guess what it is).

    I can, OTOH, discuss the reason why this was deprecated: figbox only exists on AxesSubplot, which is a dynamically generated subclass of AxesBase and the SubplotBase mixin, which makes it rather tricky to explain and requires jumping through various hoops in the codebase, even though it is by far the most widely used Axes class. To simplify that, I plan to move nearly all of SubplotBase's functionality into AxesBase, so that there will be a plain class Axes(...): ... in the codebase which will be exactly the Axes class that everyone uses. In preparation for doing so, I also dropped some APIs that seemed specific to SubplotBase, for which I believed that there were suitable replacements (e.g. figbox -> get_subplotspec().get_position(...)).

  5. jklymak commented on Apr 16, 2021

    @jklymak
    Member

    Yes, this issue would be better accompanied by a non pdb reproducible example, and some explanation of why the user needs figbox for in the first place.

  6. 2sn commented on Apr 16, 2021

    @2sn
    Author

    @anntzer Maybe the figbox can be added as a property the the new Axes base class. As I said, you need to replace figbox by BboxTransformFrom(ax.figure.bbox).transform_bbox(ax.bbox) not as you suggested above. Let me see whether I can isolate the specific code that causes the difference.

  7. 2sn commented on Apr 16, 2021

    @2sn
    Author

    Please try the following code in matplotlib 3.3.4 and 3.4.1:

    import matplotlib.pylab as plt
    import numpy as np
    from matplotlib.transforms import BboxTransformFrom
    
    fig = plt.figure()
    ax = fig.add_subplot()
    box = np.array([0.1,0.2,0.3,0.4], dtype = np.float64)
    ax.set_position(box)
    ax.get_subplotspec().get_position(ax.figure)
    BboxTransformFrom(ax.figure.bbox).transform_bbox(ax.bbox)
    ax.figbox

    You will see the last line produces the same result as the second-to-last line in matplotlib 3.3.4 but the same as the third last line in matplotlib 3.4+.

    Therefore the replacement for ax.figbox needs to be BboxTransformFrom(ax.figure.bbox).transform_bbox(ax.bbox).

  8. 2sn commented on Apr 16, 2021

    @2sn
    Author

    @jklymak ax.figbox is needed to find the normalised position of the actual current bounding box.

    Or, maybe, ax.set_position(box) does not update actual bounding box correctly? But how, then, can BboxTransformFrom(ax.figure.bbox).transform_bbox(ax.bbox) find the actual correct value?

  9. anntzer commented on Apr 16, 2021

    @anntzer
    Contributor

    OK, so if I got this correctly, you're taking a subplot (which is something intended to be placed in a grid(spec) of subplots) and forcing its position, disregarding the gridspec placement. Which is likely why the suggested alternative does not work (it assumes you're relying on gridspec placement). (We can argue about intended usages, but that's not the point here; I'm just explaining my understanding of what happened.)

    Does the simpler ax.get_position() work for you instead?

  10. 2sn commented on Apr 16, 2021

    @2sn
    Author

    @anntzer
    Yes, that also seems to work.

  11. 2sn commented on Apr 16, 2021

    @2sn
    Author

    The original code was written a very long time ago, maybe a decade. Not sure ax.get_position existed then...or was not the obvious thing to use. How would you generally create an axis in a figure w/o use of gridspec or add_subplot / subplot? All examples practical examples just use add_subplot. For 99% of my applications I do not care about gridspec whatsoever. (I would tend to say, for at least 100% my applications I do not care about gridspec.)

  12. jklymak commented on Apr 16, 2021

    @jklymak
    Member

    fig.add_axes([l, b, w, h]) is the most straight forward way....

  13. jklymak commented on Apr 16, 2021

    @jklymak
    Member

    BTW, you should give gridspec a chance - layout has gotten a lot better in the last 10 years, though I agree that there are still certain situations where manual placement is better. However, it might be helpful if you detailed those (perhaps at discourse.matplotlib.org) so we can look at improving things.

  14. 2sn commented on Apr 16, 2021

    @2sn
    Author

    I see this does not work with tight_layout. Requires gridspec. Even for single plots. It would be good to have an easy way to to reliably get rid of whitespace. tight_layout also sometimes behaves very wired. One can see this when making movies, things wiggle around w/o any obvious need. This is when you want to set axes locations to fixed values.

    I need the location info to make the colour bar in figures as attached.

    image

  15. jklymak commented on Apr 16, 2021

    @jklymak
    Member

    One trick for animations is to call tight_layout or constrained_layout once, and then turn it off; of course some artists might get clipped or overlap at that point, but that can happen if you hard-code your positions.

    Using an inset_axes for a colorbar placement can be effective.

    But for a simple layout like above adding the axes manually is fine too.

  16. 2sn commented on Apr 16, 2021

    @2sn
    Author

    @jklymak I had not heard about inset_axes before, that may be useful.

    As for animations, I wrote my own movie framework that can make frames in parallel, rather useful on SMP machines, but requires independent exact axis locations.

  17. jklymak commented on Apr 16, 2021

    @jklymak
    Member

    Is it OK to close this though? I agree that figbox is not doing what it used to do, but that is a weird property that was really only supposed to be used for subplots. I do a lot with axes layout and I didn't even know it existed, so I'm not sure we need to do a song and dance to fix it. OTOH, we shouldn't break things willy-nilly, so if we think this was being used substantially in the wild, I guess we should fix it... Maybe @tacaswell should chime in, because he tends to be conservative about these things (@anntzer and I are the wrong ones to judge 😉)

  18. 2sn commented on Apr 16, 2021

    @2sn
    Author

    May I suggest to replace the output by that of ax.get_position() instead of what it does now, for past compatibility, and adjust the deprecation warning accordingly? Hopefully that would be a very small fix.

    (obviously, my code has now been adjusted, but with the right message on what to replace with, it would have been a lot faster - never mind my non-standard use of the API)

  19. tacaswell commented on Apr 16, 2021

    @tacaswell
    Member

    Given that we know the text of the deprecation warning is confusing, we definitely should re-word it to point at ax.get_position() and it is worth fixing the value of figbox if we changed its return value.

    My knee-jerk guess is that the bounding boxes / transforms are things that may get updated as part of a draw pass so checking before/after the first draw may matter.

    One slight concern I have is that the Axes position gets a little funny when set_aspect is involved and we track both the original and adjusted positions.


    While I'm sorry we broke things on you @2sn (and thank you for reporting them!) this is why we deprecate things rather than just removing them. We have no telemetry in the library we really do not know all of the clever things are users are doing / have discovered.

  20. added this to the v3.4.2 milestone on Apr 16, 2021
  21. 2sn commented on Apr 17, 2021

    @2sn
    Author

    @tacaswell Thanks!

  22. modified the milestones: v3.4.2, v3.4.3 on May 5, 2021
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions