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
{{ message }}
Repository navigation
regression for 3.4.x - ax.figbox replacement incompatible to all version including 3.3.4 #19982
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
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(...)).
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.
@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.
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).
@jklymakax.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?
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?
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.)
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.
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.
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.
@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.
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 😉)
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)
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.
Activity
2sn commentedon Apr 16, 2021
related to #19938
2sn commentedon Apr 16, 2021
OK, it should be possible to do this, what I found is (for MPL v. 3.3.4 behaviour)
But why not just leave this as a
figboxproperty forax?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)
[-]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[/+]anntzer commentedon Apr 16, 2021
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:
figboxonly exists on AxesSubplot, which is a dynamically generated subclass ofAxesBaseand theSubplotBasemixin, 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 ofSubplotBase's functionality intoAxesBase, so that there will be a plainclass 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 toSubplotBase, for which I believed that there were suitable replacements (e.g.figbox->get_subplotspec().get_position(...)).jklymak commentedon Apr 16, 2021
Yes, this issue would be better accompanied by a non
pdbreproducible example, and some explanation of why the user needsfigboxfor in the first place.2sn commentedon Apr 16, 2021
@anntzer Maybe the
figboxcan be added as a property the the newAxesbase class. As I said, you need to replacefigboxbyBboxTransformFrom(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.2sn commentedon Apr 16, 2021
Please try the following code in matplotlib 3.3.4 and 3.4.1:
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.figboxneeds to beBboxTransformFrom(ax.figure.bbox).transform_bbox(ax.bbox).2sn commentedon Apr 16, 2021
@jklymak
ax.figboxis 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, canBboxTransformFrom(ax.figure.bbox).transform_bbox(ax.bbox)find the actual correct value?anntzer commentedon Apr 16, 2021
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?2sn commentedon Apr 16, 2021
@anntzer
Yes, that also seems to work.
2sn commentedon Apr 16, 2021
The original code was written a very long time ago, maybe a decade. Not sure
ax.get_positionexisted then...or was not the obvious thing to use. How would you generally create an axis in a figure w/o use of gridspec oradd_subplot/ subplot? All examples practical examples just useadd_subplot. For 99% of my applications I do not care aboutgridspecwhatsoever. (I would tend to say, for at least 100% my applications I do not care aboutgridspec.)jklymak commentedon Apr 16, 2021
fig.add_axes([l, b, w, h])is the most straight forward way....jklymak commentedon Apr 16, 2021
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.
2sn commentedon Apr 16, 2021
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_layoutalso 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.
jklymak commentedon Apr 16, 2021
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_axesfor a colorbar placement can be effective.But for a simple layout like above adding the axes manually is fine too.
2sn commentedon Apr 16, 2021
@jklymak I had not heard about
inset_axesbefore, 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.
jklymak commentedon Apr 16, 2021
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 😉)
2sn commentedon Apr 16, 2021
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)
tacaswell commentedon Apr 16, 2021
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_aspectis 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.
2sn commentedon Apr 17, 2021
@tacaswell Thanks!