受@theo回答的启发,我编写了一个脚本来将窗口移动并调整大小到屏幕上特定的标准位置。这已经在Qt4Agg后端进行了测试:
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()
py = mgr.canvas.height()
px = mgr.canvas.width()
d = 10
if position == "top":
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__':
plt.figure(1)
plt.plot([0, 1])
move_figure("top-right")
plt.figure(2)
plt.plot([0, 3])
move_figure("bottom-right")
fig, ax = subplots()
是不起作用的。它必须是fig, ax = plt.subplots()
。然后,在mngr.window.setGeometry()
上,我得到了“# AttributeError: SetPosition”。 - ApostolosAttributeError:'_tkinter.tkapp'对象没有属性'setGeometry'
。 - AdriaannewX + 2
,高度将会是newY + 32
,并且可能会部分超出屏幕。 - sandyscott