How to Set the Absolute Position of Figure Windows with Matplotlib

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

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.

Python on windows, open plot windows next to each other

You can choose the location of your plot but it is dependant on backend. To check this,

import matplotlib
matplotlib.get_backend()

and then see this answer for various ways to adjust.
For example, this works for me on linux with Qt4Agg,

import matplotlib.pyplot as plt

#Choose the correct dimensions for your first screen
FMwidth = 1280
FMheight = 1024

#Choose the correct dimensions for your second screen
SMwidth = 1280
SMheight = 1024

fm = plt.get_current_fig_manager()
#Works with QT on linux
fm.window.setGeometry(FMwidth,0,SMwidth,SMheight)

This may be better for windows

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

You may also be able to get the screen size(s) from,

from win32api import GetSystemMetrics

width = GetSystemMetrics(0)
weight = GetSystemMetrics(1)

You can easily create a counter which increments whenever you create a plot and adjusts this specified position so the next plot is next to the previous one each time. However, the size and layout are much easier if you use subplots, for example to set up your 2 by 3 grid,

#Setup 2 by 3 grid of subplots
fig, axs = plt.subplots(2,3,figsize=(width,height))

axs[0,0].plot(x, np.sinc(x))
axs[1,0].hist(y)
etc

You can then use your counter to specify which plot you are currently using and increment this whenever you plot.

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

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.



Related Topics



Leave a reply



Submit