python - 如何确定 matplotlib 中的屏幕大小

标签 python matplotlib

我正在寻找一种使用 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")

最佳答案

这适用于 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 像素。

关于python - 如何确定 matplotlib 中的屏幕大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29087072/

相关文章:

python - 如何在将python函数绑定(bind)到对象之前检查它是否是方法?

python - 如何将条形图值更改为百分比 (Matplotlib)

python - 条形图 - 在一列 pandas 上绘制多行

python - 在 matplotlib 中用矩形插值绘制一条线

python - 记录进程的 STDIN 和 STDOUT

python和静态方法

python - 什么时候在模块中执行语句?

python - 如何使用 scipy 执行具有亚像素精度的图像互相关

python - 修改 Matplotlib PatchCollection 中的特定补丁

python - 如何在matplotlib中填充分散区域?