39

I'm working with python and matplotlib on mac os x. When I'm working on many different windows and I have to run a script which produces a plot, the plot window always open behind the active window and is very frustration having to switch between windows for looking at the image. Is it any why to decide the location of the plot window, and/or pop up it as foreground window?

thanks

Luca Fiaschi
  • 3,145
  • 7
  • 31
  • 44

10 Answers10

17

For me (OSX 10.10.2, Matplotlib 1.4.3), what works is changing the matplotlib backend to TkAgg. Before importing pyplot or anything, go:

import matplotlib
matplotlib.use('TkAgg')  

Plot windows now pop-up, and can be Command-Tab'ed to.

Peter
  • 12,274
  • 9
  • 71
  • 86
14

I was bothered by exactly the same problem. I found finally a solution (in pylab mode, with qt4agg backend):

get_current_fig_manager().window.raise_()

or

fig = gcf()
fig.canvas.manager.window.raise_()

Regards, Markus

mrossi
  • 437
  • 6
  • 8
  • 4
    The FigureManager doesn't have anymore a window property, also I cannot find raise_() reference in API or in matplotlib source with macosx, tkagg and agg. – anddam Jan 16 '14 at 09:00
  • It still works for the Qt4Agg backend. I will edit the answer. – mrossi Jan 17 '14 at 11:15
  • 1
    When running from the command-line, this puts the plot above the terminal window (unlike several of the other ways that I tried). For those (like me) who did not know this in advance, to use the QtAgg backend, put this before importing pyplot or pylab: `import matplotlib; ; matplotlib.use('Qt4Agg')` – sdenham Mar 16 '15 at 18:38
  • Works on MacOS as long as you set SpyderPrefs to use the aforementioned backend for matplotlib. – Demis May 24 '16 at 22:55
  • 1
    When using backend TkAgg, do this `fig.canvas.manager.window.tkraise()` – user3450049 Aug 15 '18 at 04:29
  • This is great in Jupyter. Once my plot is done: `plt.gcf().canvas.manager.window.raise_()` – eric Mar 01 '22 at 18:20
7

I found this solution was so often needed (e.g. when using Spyder IDE), I wrapped it into a function.

def show_plot(figure_id=None):    
    if figure_id is None:
        fig = plt.gcf()
    else:
        # do this even if figure_id == 0
        fig = plt.figure(num=figure_id)

    plt.show()
    plt.pause(1e-9)
    fig.canvas.manager.window.activateWindow()
    fig.canvas.manager.window.raise_()
raysabr
  • 146
  • 1
  • 3
  • Tiny nitpick: "if figure_id is not None" could be contracted to "if figure_id" – slehar Sep 29 '16 at 11:35
  • 2
    I updated the function to reorder the if-else and hopefully clarify that we must test specifically for None, because figure_id == 0 is an allowed value. This should be consistent with pervasive use of None as a default argument value with subsequent test for None to initiate default behaviors. – raysabr Oct 01 '16 at 02:29
3

I found a good answer on this thread: How to make a Tkinter window jump to the front?

Basically, the idea is to use window attributes - set the '-topmost' attribute to True (1) to make the window come to the foreground, and then set it to False (0) so that it later allows other windows to appear in front of it. Here's code that worked for me:

import matplotlib.pyplot as plt
wm = plt.get_current_fig_manager() 
wm.window.attributes('-topmost', 1)
wm.window.attributes('-topmost', 0)
Community
  • 1
  • 1
2

For MacOS Sierra and python 3.6, Matplotlib 2.0.0

import matplotlib.pyplot as plt
plt.get_current_fig_manager().show()

the above line does the job no need of anything else.

James Paul Mason
  • 1,051
  • 1
  • 13
  • 27
H S Rathore
  • 1,954
  • 2
  • 15
  • 20
  • 1
    This worked for me too. Took 30 minutes of googling with multiple things that seemed like they _should_ work but didn't until I came across this excellent one-liner that did the trick. – James Paul Mason Dec 09 '19 at 23:50
  • @JamesPaulMason.. Glad I could help..Thanks for the edit. – H S Rathore Apr 30 '20 at 07:49
1

This worked for me!!

(Tested on Mac OS X 10.11, Spyder 2.3.5.2 - Python 3.4)

Go to Preferences > IPython console > Graphics and set a backend to Qt (after that you need to restart the kernel).

Make a file that contains:

def raise_window(figname=None):
    if figname: plt.figure(figname)
    cfm = plt.get_current_fig_manager()
    cfm.window.activateWindow()
    cfm.window.raise_()

and import it at startup (Preferences > IPython console > Startup > Run a file). Now, just call function raise_window() below your code.

Example:

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)

plt.figure()
plt.plot(X, C)
plt.plot(X, S)

raise_window()
DomenR
  • 11
  • 1
  • 2
1

For me only the following works (with TkAgg backend):

plt.gcf().canvas.get_tk_widget().focus_force() 
Apostolos
  • 3,115
  • 25
  • 28
0

As of matplotlib 1.5.1 on MacOSX 10.11.6, if you start an iPython (5.0.0, Python: 3.5.2) shell and use %matplotlib you can bring a matplotlib plot to the front using:

>>> %matplotlib
Using matplotlib backend: MacOSX
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,3,2,4])
>>> plt.show()

** Edit: Advice seems to be not to use %pylab as it pollutes the global name space **

.. shell and use %pylab you can bring a matplotlib plot to the front using:

>>> %pylab
Using matplotlib backend: MacOSX
Populating the interactive namespace from numpy and matplotlib
>>> plot([1,3,2,4])
>>> show()
AJP
  • 26,547
  • 23
  • 88
  • 127
0

You can set

backend      : MacOSX

in your matplotlibrc file for a permanent solution.

It works for me on macos mojave, with matplotlib 2.1.2. However, other users have complained that it does not work for them, so it might be affected by other settings

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

The following worked on Jupyter notebook with Qt5 backend on Windows. I tested it with Python 3.7, matplotlib 3.2.1.

%matplotlib qt5
import matplotlib.pyplot as plt
import numpy as np
from PyQt5 import QtCore
plt.plot(np.linspace(0,1))
window = plt.get_current_fig_manager().window
window.setWindowFlags(window.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
plt.show()
window.setWindowFlags(window.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
plt.show()
perimasu
  • 1,025
  • 1
  • 8
  • 16