11

When I create a new figure using pyplot, it opens automatically on the top left of my screen. I would like it to open at another position (for example top right of my screen). What I have been doing so far is change the position afterwards using:

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)

Is there any way I could set the desired position as default to Matplotlib? I looked up in the matplotlibrc file but found nothing that could help me... any ideas?

4
  • The problem is that the method for moving a window is different for different backends. So I don't think there is a generic way of doing this, hence why it doesn't show up in the matplotlibrc file. See stackoverflow.com/questions/7449585/… Commented Feb 22, 2017 at 14:23
  • Thanks, but the topic you mention is about setting the position manually, and I would like to set it once for all as default parameters. I have to find out where these defaults are set, and whether one can override them... Commented Feb 22, 2017 at 14:36
  • My point is that because it is done differently for all backends, I don't think there is a way to globally override them. But good luck looking... Commented Feb 22, 2017 at 15:14
  • I don't think there is something like a default setting anywhere. In my case e.g. the window always appears somewhere in the middle of the screen. This is probably controlled by the operating system not by python or matplotlib. Commented Feb 22, 2017 at 15:19

2 Answers 2

5

Thank you guys for your answers. I understand these defaults are not governed by Python. The solution I found is to define a function to open a new figure and move it in the right place. This way, although I have to define or import the function each time I open a ipython console, I will not have to move each figure afterwards:

# 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)
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you are using iPython I came up with the following hack.

Write this in your startup.py in ~/.ipython/profile_default/startup/

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

This way, all plots created by matplotlib are automatically created at the given position.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.