如何更改matplotlib图形的默认窗口位置?

11

当我使用pyplot创建新的图形时,它会自动打开在我的屏幕左上角。我想让它在另一个位置打开(例如在屏幕右上角)。 到目前为止,我一直在使用以下方法来更改位置:

import matplotlib.pyplot as plt
plt.figure()    # opens on the top left
(x,y,w,h) = ... # The desired position
plt.get_current_fig_manager().window.setGeometry(x,y,w,h)

有没有办法将所需位置设置为Matplotlib的默认值? 我查找了matplotlibrc文件,但找不到任何能帮助我的东西 ... 有什么想法吗?


问题在于不同后端的窗口移动方法不同。因此,我认为没有通用的方法来解决这个问题,这也是为什么它不会出现在 matplotlibrc 文件中的原因。请参见 https://dev59.com/cms05IYBdhLWcg3wGuKd?rq=1 - tmdavison
谢谢,但你提到的话题是关于手动设置位置,而我想将其一次性设置为默认参数。我必须找出这些默认值是在哪里设置的,以及是否可以覆盖它们... - user7605211
我的观点是,由于每个后端的实现方式都不同,我认为没有一种全局覆盖它们的方法。但祝你好运... - tmdavison
我认为没有任何默认设置。例如,在我的情况下,窗口总是出现在屏幕中央的某个位置。这可能是由操作系统控制而不是由Python或Matplotlib控制。 - ImportanceOfBeingErnest
2个回答

5

感谢大家的回答。我理解这些默认值不受Python控制。 我找到的解决方案是定义一个函数来打开一个新图并将其移动到正确的位置。这样,虽然我每次打开ipython控制台都必须定义或导入该函数,但我不需要再移动每个图:

# in file mymodule.py
import matplotlib.pyplot as plt

def newfigure(num=None):
       hfig = plt.figure(num)
       plt.get_current_fig_manager().window.setGeometry(x,y,w,h)
       return hfig

# in a python script, or in the interactive console:
import matplotlib.pyplot as plt
import mymodule as mm

plt.figure()   # Opens where I don't want it to
mm.newfigure() # Opens at position (x,y) and with width and height (w,h)

0
假设您正在使用iPython,我想到了以下的hack。
请在~/.ipython/profile_default/startup/中的startup.py文件中编写此内容。
import matplotlib.pyplot as plt
def _new_figure(*args, **kwargs):
    """
    This hack that creates a figure and position it
    """
    fig = plt._figure(*args, **kwargs)
    fig.canvas.manager.window.move(0)
    return fig

plt._figure = plt.figure
plt.figure = _new_figure

这样,所有由matplotlib创建的图都会自动在给定位置创建。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接