-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Feature request: pre_draw_event #5042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Why? |
Why not?
|
To avoid API bloat. |
Just to clarify my use case - this came about because I was working on finding a way to update the position of the axes before each draw event (see my emails on the mailing list yesterday). The solution I came up with looks something like this: https://gist.github.com/astrofrog/8d579ea83e578a9cdb99 where I basically have to subclass the axes class. However, I wanted to find a way to do this that would not require subclassing axes, because I want it to be applicable to various existing Axes subclasses, and don't want to have to subclass each of them. So the natural solution is to use events and do something like this: class AxesResizer(object):
def __init__(self, ax):
self.ax = ax
bbox = ax.get_position()
self._orig_width = ax.figure.get_figwidth()
self._orig_height = ax.figure.get_figheight()
self._orig_rect = [bbox.x0, bbox.y0, bbox.width, bbox.height]
def on_draw(self, event):
new_width = self.ax.figure.get_figwidth()
new_height = self.ax.figure.get_figheight()
x0 = self._orig_rect[0] * self._orig_width / new_width
y0 = self._orig_rect[1] * self._orig_height / new_height
w = max(0.01, 1 - (1 - self._orig_rect[2]) * self._orig_width / new_width)
h = max(0.01, 1 - (1 - self._orig_rect[3]) * self._orig_height / new_height)
self.ax.set_position([x0, y0, w, h])
if __name__ == "__main__":
fig = plt.figure()
ax = plt.Axes(fig, [0.1, 0.1, 0.8, 0.8])
fig.add_axes(ax)
ax.plot([1, 2, 3])
r = AxesResizer(ax)
fig.canvas.mpl_connect('draw_event', r.on_draw)
plt.show() However, Trying This led me to think that in some cases such as this, once the canvas has been drawn, it's already too late, and in many cases maybe one would rather know when the canvas is about to be drawn. One could then e.g. update positions for an animation, or adjust the axes position at the last minute (like in my use case). |
Then I think we should fix resize_event on the backends where it doesn't work. |
@astrofrog What other backends does in not work on? |
@tacaswell - it used not to work for me for |
At the moment
draw_event
is triggered after a draw, but it would be great to be able to specify callbacks that happen just before the drawing is done. Is this something that would be easy to implement? (e.g. aspre_draw_event
, and maybe renamedraw_event
topost_draw_event
with a backward-compatible alias?)The text was updated successfully, but these errors were encountered: