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

时间:2017-02-22 14:19:40

标签: python matplotlib

当我使用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文件,但发现没有什么可以帮助我...任何想法?

2 个答案:

答案 0 :(得分:1)

谢谢你们的回答。我知道这些默认值不受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)

答案 1 :(得分:0)

假设您使用的是iPython,我想到了以下技巧。

在〜/ .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创建的所有图都会自动在给定位置创建。

相关问题