Python 如何在matplotlib中确定屏幕大小

Python 如何在matplotlib中确定屏幕大小,python,matplotlib,Python,Matplotlib,我正在寻找一种使用matplotlib和任何交互式后端(例如TkAgg、Qt4Agg或macosx)获得屏幕大小(以像素为单位)的通用方法 我正在尝试编写一个函数,可以在屏幕上的一组标准位置打开窗口,例如屏幕的右半部分或右上角 我编写了一个工作解决方案,复制如下,但它需要使用全屏切换()来创建一个全屏窗口来测量其大小 import matplotlib.pyplot as plt def move_figure(position="top-right"): ''' Move a

我正在寻找一种使用matplotlib和任何交互式后端(例如TkAgg、Qt4Agg或macosx)获得屏幕大小(以像素为单位)的通用方法

我正在尝试编写一个函数,可以在屏幕上的一组标准位置打开窗口,例如屏幕的右半部分或右上角

我编写了一个工作解决方案,复制如下,但它需要使用全屏切换()来创建一个全屏窗口来测量其大小

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")
我正在寻找一种方法来获得屏幕大小,而不创建一个全屏窗口,然后改变它的大小

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")
这适用于OS X:

import AppKit
[(screen.frame().size.width, screen.frame().size.height) for screen in AppKit.NSScreen.screens()]
类上的Apple文档。

这适用于TKagg

import matplotlib.pyplot as plt

window = plt.get_current_fig_manager().window
screen_x, screen_y = window.wm_maxsize()

#or
screen_y = window.winfo_screenheight()
screen_x = window.winfo_screenwidth()

我所看到的唯一奇怪的事情是,有时y的大小约为22像素。

这是否意味着回答帮助重新标记,因为问题根本不依赖于mpl,只依赖于Qt。@VidhyaG您的解决方案可以工作。谢谢然而,我不是只找窗户solution@tcaswell如果我试图用matplotlib实现的目标无法实现,那么在回答中解释这一点会很有用。但是,我不确定我的问题的解决方案如何“完全不依赖matplotlib”。get_current_fig_manager()和full_screen_toggle()不是matplotlib命令吗?我假设如果我使用的是另一个绘图库,例如Chaco,我将不得不使用不同的命令。也许我误解了。。。请澄清,以及适当解决方案的可能方向(如果有)。我误解了您的问题,我以为您是在
Qt
中专门询问如何执行此操作的。我怀疑这将依赖后端。谢谢。不幸的是,这也是特定于操作系统的。使用
winfo\u screenheight()
的解决方案提供了正确的大小,而没有偏移
wm\u maxsize()
是。这就是我说“或”的原因。您的回答没有说明“奇怪的事情”只指第一个解决方案。有什么原因没有将其标记为解决方案吗?因为它是特定于后端的。不幸的是,matplotlib中似乎不存在一个通用的解决方案,它可以与任何交互式后端一起工作。