【发布时间】:2015-03-16 21:17:00
【问题描述】:
我正在寻找一种使用 matplotlib 和任何交互式后端(例如 TkAgg、Qt4Agg 或 macosx)来获取以像素为单位的屏幕大小的通用方法。
我正在尝试编写一个函数,它可以在屏幕上的一组标准位置打开一个窗口,例如屏幕的右半边,或右上角。
我写了一个有效的解决方案here,复制到下面,但它需要使用 full_screen_toggle()(建议 here)来创建一个全屏窗口来测量其大小。
我正在寻找一种无需创建全屏窗口然后更改其大小即可获得屏幕大小的方法。
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")
【问题讨论】:
-
重新标记为问题根本不依赖于 mpl,只依赖于 Qt。
-
@VidhyaG 您的解决方案会奏效。谢谢。但是我不是在寻找仅限 Windows 的解决方案
-
@tcaswell 如果我试图实现的目标不能用 matplotlib 完成,那么在答案中解释这一点会很有用。但是我不确定我的问题的解决方案如何“完全不依赖于 matplotlib”。 get_current_fig_manager() 和 full_screen_toggle() 不是 matplotlib 命令吗?我假设如果我使用其他绘图库,例如,我将不得不使用不同的命令。查科。也许我误解了......如果有的话,我们将不胜感激。
-
我误解了你的问题,我以为你在
Qt中专门询问有关这样做的问题。我怀疑这将取决于后端。
标签: python matplotlib