axes.get_position() is pretty simple, but always returns the frozen version of the axes position...
defget_position(self, original=False):
'Return the a copy of the axes rectangle as a Bbox'iforiginal:
returnself._originalPosition.frozen()
else:
returnself._position.frozen()
However, in axes.apply_aspect() this code is executed that changes the box size:
I don't understand why in both cases the frozen position is returned in ax.get_position() rather than the current position. But maybe a new method get_adjusted_position() would work if we don't want to change get_position()?
.. and interestingly ax.apply_aspect isn't called when ax.set_aspect is called. I actually think that is a bug/overshight: we already know everything about the figure when ax.set_aspect is called that apply_aspect needs. Of course the figure can change size, so it should be called again at draw time, but it is useful to know the position of the axes at the initial draw.
It is not an oversight; you don't necessarily know everything you need to know until draw time. In a situation where the necessary information is available, the user is free to trigger a draw, or to call ax.apply_aspect manually, before calling get_position(). Aspect ratio handling is a complicated and costly operation, so I don't favor triggering it within set_aspect, which can be long before the necessary information is available.
I agree that things can change after, particularly if sharex or sharey are used, but it is a little mysterious to not know what the new positions is without calling ax.apply_aspect(). If it makes sense, I think I'll make a little PR that notes this in the doc-string:
The position of the axes that has had its aspect ratio changed is not set until draw time,
or after a manual call to ax.apply_aspect() has been made.
The documentation change is a good idea.
It might make sense to track whether a call to apply_aspect is needed, and call it if needed, within get_position(original=False), but I haven't investigated that.
Thats an interesting idea. I'm also not sure why apply_aspect() would be considered expenisve that we would need to keep track of whether it was called or not. But I don't have any experience optimizing the project...
Agreed, I don't know how expensive it is, and maybe it is negligible; but it's a monstrosity, so I guess I just don't like the idea of calling it unnecessarily.
Activity
jklymak commented on Sep 20, 2017
.. and interestingly ax.apply_aspect isn't called when ax.set_aspect is called. I actually think that is a bug/overshight: we already know everything about the figure when ax.set_aspect is called that apply_aspect needs. Of course the figure can change size, so it should be called again at draw time, but it is useful to know the position of the axes at the initial draw.
efiring commented on Sep 20, 2017
It is not an oversight; you don't necessarily know everything you need to know until draw time. In a situation where the necessary information is available, the user is free to trigger a draw, or to call ax.apply_aspect manually, before calling get_position(). Aspect ratio handling is a complicated and costly operation, so I don't favor triggering it within set_aspect, which can be long before the necessary information is available.
jklymak commented on Sep 20, 2017
@efiring
Ah, OK, of course ax.apply_aspect() is a public method. Thats great!
yields as you say:
I agree that things can change after, particularly if sharex or sharey are used, but it is a little mysterious to not know what the new positions is without calling ax.apply_aspect(). If it makes sense, I think I'll make a little PR that notes this in the doc-string:
efiring commented on Sep 20, 2017
The documentation change is a good idea.
It might make sense to track whether a call to apply_aspect is needed, and call it if needed, within get_position(original=False), but I haven't investigated that.
jklymak commented on Sep 20, 2017
Thats an interesting idea. I'm also not sure why apply_aspect() would be considered expenisve that we would need to keep track of whether it was called or not. But I don't have any experience optimizing the project...
efiring commented on Sep 20, 2017
Agreed, I don't know how expensive it is, and maybe it is negligible; but it's a monstrosity, so I guess I just don't like the idea of calling it unnecessarily.