受@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() # 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")