Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the default window position of a matplotlib figure?

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?

like image 927
user7605211 Avatar asked Feb 22 '17 14:02

user7605211


People also ask

How do I change the plot window in matplotlib?

Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.

How do I change the location of a matplotlib legend?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do I change the position of a title in matplotlib?

In this method, we will be using the pad argument of the title() function to change the title location in the given plot in the python programming language. Example: In this example, We will elevateTitleTitle by using the “pad” parameter. The offset of the Title from the top of the axes, in points.


1 Answers

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)
like image 59
user7605211 Avatar answered Oct 17 '22 20:10

user7605211