59

I'm writing a simple Python application that uses matplotlib to display a few figures on screen. The number of figures generated is based on user input and changes throughout the application's life. The user has the ability to issue a "plot" command to generate a new figure window with the selected data series. In order to improve the user experience, I would like to provide another command that would programmatically arrange all open figure windows in some convenient arrangement (e.g. tile them across the available screen space).

I believe to have found APIs that allow me to adjust the size of the figure window (in pixels), but haven't had any success in finding a way to set their absolute position on screen. Is there a way to do this without delving into the details of whatever backend is in use? I would like to do this in a backend-agnostic way so I can avoid relying upon implementation details that might change in the future.

Jason R
  • 11,159
  • 6
  • 50
  • 81

11 Answers11

54

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)
starball
  • 20,030
  • 7
  • 43
  • 238
K.-Michael Aye
  • 5,465
  • 6
  • 44
  • 56
  • 6
    note that instead of mngr = get_current_fig_manager(), we can also use fig.canvas.manager – poppie Feb 22 '17 at 09:30
  • 2
    First of all, `fig, ax = subplots()` doesn't work. It must be `fig, ax = plt.subplots()`. Then, on `mngr.window.setGeometry()`, I get "# AttributeError: SetPosition". – Apostolos Apr 07 '18 at 08:13
  • First of all, thanks for the friendly pointer to the bug in the code. Second, I don't understand how the use of the attribute "setGeometry()" leads to an attribute error mentioning SetPosition. Possibly, your PyQT isn't activated as the backend. Try `ipython --pylab=pyqt5` to make sure pyqt is active. – K.-Michael Aye Apr 07 '18 at 17:36
  • In Pycharm I get `AttributeError: '_tkinter.tkapp' object has no attribute 'setGeometry'` – Adriaan May 25 '18 at 08:36
  • You are in the wrong post, this post is the QT solution, you obviously work with tkinter. – K.-Michael Aye May 25 '18 at 18:31
  • 1
    Another note: this positions the figure area itself, the title bar and borders (provided by the OS) are extra. On Windows 10 with default settings, this is a 30px title bar and 1 px border all the way round, so your window will be `newX + 2` wide and `newY + 32` tall, and could be partially outside the screen. – sandyscott Jun 11 '20 at 17:21
  • `AttributeError: 'FigureManagerBase' object has no attribute 'window'` – Trenton McKinney Sep 02 '23 at 20:00
38

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()
cxrodgers
  • 4,317
  • 2
  • 23
  • 29
  • 1
    Hey, I just tried this and got an error: `AttributeError: 'AxesSubplot' object has no attribute 'canvas'` – Rich Oct 03 '16 at 04:51
  • This is the only answer here that would work for me, specifically using `f.canvas.manager.window.move()`. The syntax is simplest as well. – pretzlstyle Feb 02 '18 at 22:36
  • 3
    Nice, but move `plt.show` outside the function, because it blocks. The main reason you might want this is specifically when you have many figures! – supergra Nov 15 '18 at 00:42
  • 1
    @supergra thanks for the tip, I edited ... I always run pyplot in interactive mode so it doesn't block for me, but no reason not to make the change – cxrodgers Nov 16 '18 at 01:11
  • 2
    Good tip. I added this to my ipython startup script. Also added `if isinstance(f,int) : f = plt.figure(f)` to the `move_figure` function so it works with figure numbers or objects. (i.e. `move_figure(1,500,500)`) – argentum2f Aug 28 '19 at 14:12
  • 1
    Thank you. This did not initially work for me on my system, which is using the "MacOSX" backend by default, but if I set things to `matplotlib.use("TkAgg")` it did. For what it is worth, I just posted [on Reddit](https://www.reddit.com/r/inventwithpython/comments/uaq5a9/window_placement_in_chapter_2_project_of_real/?utm_source=share&utm_medium=web2x&context=3) about my learning experience here. – Jeffrey Goldberg Apr 24 '22 at 08:29
  • @JeffreyGoldberg if you can figure out the syntax for MacOSX backend, I will add it to the answer as another option. Unfortunately I don't have access to a Mac to discover the proper syntax myself – cxrodgers Apr 24 '22 at 23:06
17

there is not that I know a backend-agnostic way to do this, but definitely it is possible to do it for some common backends, e.g., WX, tkagg etc.

import matplotlib
matplotlib.use("wx")
from pylab import *
figure(1)
plot([1,2,3,4,5])
thismanager = get_current_fig_manager()
thismanager.window.SetPosition((500, 0))
show()

per @tim at the comment section below, you might wanna switch to

thismanager.window.wm_geometry("+500+0")

instead. For TkAgg, just change it to

thismanager.window.wm_geometry("+500+0")

So I think you can exhaust through all the backends that are capable of doing this, if imposing a certain one is not an option.

nye17
  • 12,857
  • 11
  • 58
  • 68
  • Thanks. I'm using TKAgg on my system, but I don't think I can assume that the end user will. I might be able to enforce that the user must use some subset of them, though. – Jason R Sep 17 '11 at 02:56
  • @Jason R it really depends on how important this specified positioning is in your application. If it were merely for improving user experience, you can simply go for whichever subset you can take care of, otherwise, can you generate only one window, and dynamically place each plot as sub_plots while erasing old sub_plots? – nye17 Sep 17 '11 at 05:54
  • 3
    Dunno why, but for me (Windows, Python Idle, Python 2.7, started from Notepad++) the `thismanager.window.wm_geometry("+500+0")` worked, the `thismanager.window.SetPosition((500, 0))` didnt ;-) – tim May 28 '14 at 14:12
  • 3
    Trying both solutions, I get `'MainWindow' object has no attribute 'SetPosition'` and `'MainWindow' has no attribute 'wm_geometry'` – pretzlstyle Feb 02 '18 at 22:31
  • 1
    I copy-pasted your code and I get the following errors: 1) on `plt.use("wx")`, I get "AttributeError: 'module' object has no attribute 'use'" 2) on `mngr.window.SetPosition()`, I get "# AttributeError: SetPosition". (I run PY ver 2.7) – Apostolos Apr 07 '18 at 08:08
  • 1
    Correction of the above comment: I got the following error on `matplotlib.use("wx")`: "AttributeError: 'module' object has no attribute 'use'". Then of course `mngr.window.SetPosition()` won't work. – Apostolos Apr 07 '18 at 09:28
8

For Qt4Agg, this worked for me.

fig = figure()
fig.canvas.manager.window.move(0,0)

Tested on Win7, mpl version 1.4.2, python 2.7.5

otterb
  • 2,660
  • 2
  • 29
  • 48
6

Inspired by @theo answer, I wrote a script to move and resize a window to a specific standard position on the screen. This was tested with the Qt4Agg backend:

import matplotlib.pyplot as plt

def move_figure(position="top-right"):
    '''
    Move and resize a window to a set of standard positions on the screen.
    Possible positions are:
    top, bottom, left, right, top-left, top-right, bottom-left, bottom-right
    '''

    mgr = plt.get_current_fig_manager()
    mgr.full_screen_toggle()  # primitive but works to get screen size
    py = mgr.canvas.height()
    px = mgr.canvas.width()

    d = 10  # width of the window border in pixels
    if position == "top":
        # x-top-left-corner, y-top-left-corner, x-width, y-width (in pixels)
        mgr.window.setGeometry(d, 4*d, px - 2*d, py/2 - 4*d)
    elif position == "bottom":
        mgr.window.setGeometry(d, py/2 + 5*d, px - 2*d, py/2 - 4*d)
    elif position == "left":
        mgr.window.setGeometry(d, 4*d, px/2 - 2*d, py - 4*d)
    elif position == "right":
        mgr.window.setGeometry(px/2 + d, 4*d, px/2 - 2*d, py - 4*d)
    elif position == "top-left":
        mgr.window.setGeometry(d, 4*d, px/2 - 2*d, py/2 - 4*d)
    elif position == "top-right":
        mgr.window.setGeometry(px/2 + d, 4*d, px/2 - 2*d, py/2 - 4*d)
    elif position == "bottom-left":
        mgr.window.setGeometry(d, py/2 + 5*d, px/2 - 2*d, py/2 - 4*d)
    elif position == "bottom-right":
        mgr.window.setGeometry(px/2 + d, py/2 + 5*d, px/2 - 2*d, py/2 - 4*d)


if __name__ == '__main__':

    # Usage example for move_figure()

    plt.figure(1)
    plt.plot([0, 1])
    move_figure("top-right")

    plt.figure(2)
    plt.plot([0, 3])
    move_figure("bottom-right")
divenex
  • 15,176
  • 9
  • 55
  • 55
3

This also works:

fig = figure()
fig.canvas.manager.window.Move(100,400)

If you want to send a plot to an image and have it open with the default image manager (which likely remembers position) use this from here:

fig.savefig('abc.png')
from PIL import Image
im = Image.open("abc.jpg")
im.rotate(0).show()
  • 1
    with small `m` in `move` –  Oct 04 '13 at 11:47
  • To combine the two previous comments: At least when using the Qt backend (Qt4Agg, and probably also Qt5Agg), the correct command to resize is `fig.canvas.manager.window.move(100,400)`. Seems to also be true for GTK3Agg, so I'll edit this answer. – Filip S. Feb 17 '16 at 07:27
3

The following worked for me.

import matplotlib  
matplotlib.use("TkAgg") # set the backend  

if backend == 'TkAgg':  
    f.canvas.manager.window.wm_geometry("+%d+%d" % (x, y))
Cryptoman
  • 201
  • 2
  • 3
  • to build upon this answer: mngr = plt.get_current_fig_manager() mngr.canvas.manager.window.wm_geometry("+%d+%d" % (x, y)) – Supamee Nov 09 '18 at 17:28
2
'''This is a way to resize the window to a given fraction of the screen.
It uses the screenSize in pixels. User specifies the fx and fy fraction
of the sreen or just a fraction. Couldn't fine how to really position the
window though. No hints in the current figmanager could be found.
But of course, this could be combined with mgr.canvas.move()

'''

import matplotlib.pyplot as plt
#pylab

def screenPos(f):
   '''reset window on screen to size given by fraction f
   where f may by a scalar or a tuple, and where all values
   are 0<=f<=1
   '''
   if type(f)==float: f=(f,) # assert we have a tuple
   mgr = plt.get_current_fig_manager()
   mgr.full_screen_toggle() # primitive but works
   py = mgr.canvas.height()
   px = mgr.canvas.width()
   mgr.resize(f[0]*px,f[-1]*py)
   return f[0]*px,f[-1]*py

px,py = screenPos(0.8)
theo olsthoorn
  • 455
  • 3
  • 6
2
def show_img(img, title=""):
    plt.imshow(img)
    plt.title(title)
    thismanager = plt.get_current_fig_manager()
    thismanager.window.wm_geometry("+100+100")
    plt.show()
leenremm
  • 1,083
  • 13
  • 19
1

For the windows platform you could install and use pyfig module from Pyfig.

Example on how to manipulate the figure windows is given below:

import pylab as p
import pyfig as fig
for ix in range(6): f = p.figure(ix)
fig.stack('all')
fig.stack(1,2)
fig.hide(1)
fig.restore(1)
fig.tile()
fig.pile()
fig.maximize(4)
fig.close('all')
Ofir Farchy
  • 7,657
  • 7
  • 38
  • 58
Per A.
  • 11
  • 1
0

How about defining a function to raise the window to the top level and move it toward the top-left corner (for example) like this:

def topfig():
    figmgr = get_current_fig_manager()
    figmgr.canvas.manager.window.raise_()
    geom = figmgr.window.geometry()
    x,y,dx,dy = geom.getRect()
    figmgr.window.setGeometry(10, 10, dx, dy)

Then whenever you open a new figure you just type "topfig()". Is there a way to pre-define topfig so it will always be available?

slehar
  • 319
  • 3
  • 6