受@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")
谢谢。我在我的系统上使用TKAgg,但我认为我不认为最终用户会这样做。不过,我可能会强制用户必须使用它们的一些子集。 –
@Jason R它真的取决于这个指定定位在您的应用程序中的重要性。如果仅仅是为了改善用户体验,那么您可以简单地选择任何可以照顾的子集,否则只能生成一个窗口,并在删除旧的子图时动态地将每个图作为子图进行布局? – nye17
不知道为什么,但是对于我来说('Windows Idle,Python 2.7,从Notepad ++开始)'thismanager.window.wm_geometry(“+ 500 + 0”)'工作,'thismanager.window.SetPosition((500,0 ))'没有;-) – tim