0

When i plot something in PyCharm using matplotlib it plots the figure in a seperate window, which is what i want, but it also opens it on the main monitor. Is there a option to open it on my second monitor?

I could not find any similar question (only questions about plotting on the same monitor without a seperate window).

Thanks in advance!

2 Answers2

0

You can specify a position in the code

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
mngr = plt.get_current_fig_manager()
# to put it into the upper left corner for example:
mngr.window.setGeometry(2000,100,640, 545)
plt.show()
ymmx
  • 4,769
  • 5
  • 32
  • 64
  • unfortunately does not work. Traceback (most recent call last): File "E:\5_Python\Pych\main1.py", line 37, in mngr.window.setGeometry(2000,100,640, 545) File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 2347, in __getattr__ return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'setGeometry' Process finished with exit code 1 – Niels de Vries Jun 03 '22 at 07:07
  • Does the code work by itself? or does it crash in your own implementation? – ymmx Jun 03 '22 at 07:11
  • Both. It does not work by itself. And also not in my own implementation. – Niels de Vries Jun 03 '22 at 07:11
  • what matlplotlib version do you have? – ymmx Jun 03 '22 at 07:12
  • Matplotlib version 3.5.2 – Niels de Vries Jun 03 '22 at 07:13
  • Ok I have the 3.5.1 because I had issue with the new one. that can explain the error – ymmx Jun 03 '22 at 07:14
  • Version 3.5.1 also produces the same error for me :( I will look further into this error. I appreciate the help! – Niels de Vries Jun 03 '22 at 07:17
  • Ok i got the code to run with this small change: mngr.window.setGeometry = (2000,100,640, 545). However, changing the numbers still does not seem to be doing anything – Niels de Vries Jun 03 '22 at 07:22
  • yes, you have just add a variable named `setGeometry` to the `mngr.window` class. This will have no impact on the window at all. the `setGeometry()` is a function that does things in it. but it seems that you don"t have the same `FigureManagerBase` object. – ymmx Jun 03 '22 at 07:25
  • can you `print( type(mngr))` just after `mngr = plt.get_current_fig_manager()`? I get `` – ymmx Jun 03 '22 at 07:26
  • Ya there seems to be a difference there: '''''' Anyway i have found a working solution in another post. Thank you for taking the time to help me! – Niels de Vries Jun 03 '22 at 07:37
  • Seems like different backend needs different code: this works for me: mngr.window.wm_geometry("+%d+%d" % (2200, 100)) – Niels de Vries Jun 03 '22 at 07:40
0

I have found a solution in another post How do you set the absolute position of figure windows with matplotlib?

def move_figure(f, x, y):
    """Move figure's upper left corner to pixel (x, y)"""
    backend = matplotlib.get_backend()
    if backend == 'TkAgg':
        f.canvas.manager.window.wm_geometry("+%d+%d" % (x, y))
    elif backend == 'WXAgg':
        f.canvas.manager.window.SetPosition((x, y))
    else:
        # This works for QT and GTK
        # You can also use window.setGeometry
        f.canvas.manager.window.move(x, y)

f, ax = plt.subplots()
move_figure(f, 2200, 500)
plt.show()