How do you set the absolute position of figure windows with matplotlib?

Solution 1:

FINALLY found the solution for QT backend:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
mngr = plt.get_current_fig_manager()
# to put it into the upper left corner for example:
mngr.window.setGeometry(50,100,640, 545)

If one doesn't know the x- and y-width one can read them out first, like so:

# get the QTCore PyRect object
geom = mngr.window.geometry()
x,y,dx,dy = geom.getRect()

and then set the new position with the same size:

mngr.window.setGeometry(newX, newY, dx, dy)

I was searching quite often for this and finally invested the 30 minutes to find this out. Hope that helps someone.

Solution 2:

With help from the answers thus far and some tinkering on my own, here's a solution that checks for the current backend and uses the correct syntax.

import matplotlib
import matplotlib.pyplot as plt

def move_figure(f, x, y):
    """Move figure's upper left corner to pixel (x, y)"""
    backend = matplotlib.get_backend()
    if backend == 'TkAgg':
        f.canvas.manager.window.wm_geometry("+%d+%d" % (x, y))
    elif backend == 'WXAgg':
        f.canvas.manager.window.SetPosition((x, y))
    else:
        # This works for QT and GTK
        # You can also use window.setGeometry
        f.canvas.manager.window.move(x, y)

f, ax = plt.subplots()
move_figure(f, 500, 500)
plt.show()