如何在Matplotlib中确定屏幕尺寸 [英] How to determine screen size in matplotlib

查看:78
本文介绍了如何在Matplotlib中确定屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a general way to get the screen size in pixels using matplotlib with any interactive backend (e.g. TkAgg, Qt4Agg, or macosx).

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

I am trying to write a function which can open a window at a set of standard locations on the screen like e.g. the right half of the screen, or the top-right corner.

我写了一个有效的解决方案这里,复制到下面,但它需要一个人使用 full_screen_toggle()(如建议的此处)创建一个全屏窗口以测量其大小.

I wrote a working solution here, copied below, but it requires one to use full_screen_toggle() (as suggested here) to create a full-screen window to measure its size.

我正在寻找一种在不创建全屏窗口然后更改其大小的情况下获取屏幕大小的方法.

I am looking for a way to get the screen size without creating a full-screen window and then changing its size.

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像素.

Only odd thing I've seen is sometimes the y size is off by about 22 px.

这篇关于如何在Matplotlib中确定屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆