6
votes

Is it possible to change the icon of a Matplotlibe figure window? My application has a button that opens a Figure window with a graph (created with Matplotlib). I managed to modify the application icon, but the figure window still has the 'Tk' icon, typical of Tkinter.

5
Not exactly what you asked for, but here is an example of embedding matplot figure in Tkinter : matplotlib.sourceforge.net/examples/user_interfaces/… (maybe overkill just to change the icon) - FabienAndre
@FabienAndre: Thanks, but I can't do what I want with that - maupertius
Since you managed to change the icon of a regular tkinter window, displaying the matplotlib figure in a tkinter widget let you show it in its own Tkinter window (Toplevel is the widget that let you open window) of which you can change icon. Moreover, you are not forced to use another window which may provide interesting design alternatives. - FabienAndre
Mmm I don't quite understand. I create my figure using imshow() from the Tkinter app (which has the icon I want). Apparently, with the imshow() command the window that pops up still has the TKinter logo, and not my app one. - maupertius
it sounds like you're trying to do this: stackoverflow.com/questions/4073660/… that is the imshow() should be invoked from an axis in a figure-object and not from the pyplot-module directly. - deinonychusaur

5 Answers

6
votes

I solved it in this way: BEFORE I press the button that creates the figure with imshow() and show(), I initialize the figure in this way:

plt.Figure()
thismanager = get_current_fig_manager()
thismanager.window.wm_iconbitmap("icon.ico")

so when I press show() the window has the icon I want.

3
votes

For me the previous answer did not work, rather the following was required:

from Tkinter import PhotoImage
import matplotlib

thismanager = matplotlib.pyplot.get_current_fig_manager()
img = PhotoImage(file='filename.ppm')
thismanager.window.tk.call('wm', 'iconphoto', thismanager.window._w, img)
1
votes

Just adding this here, now that the Qt5Agg backend has made it's way into the mainstream. It's similar (pretty much the same) to the Qt4Agg backend as outlined by Sijie Chen's answer.

import os
import matplotlib.pyplot as plt
from PyQt5 import QtGui

# Whatever string that leads to the directory of the icon including its name
PATH_TO_ICON = os.path.dirname(__file__) + '/static/icons/icon.ico'

plt.get_current_fig_manager().window.setWindowIcon(QtGui.QIcon(PATH_TO_ICON))
0
votes

If you are using Qt4Agg backend, the following code may help you:

thismanager = plt.get_current_fig_manager()
from PyQt4 import QtGui
thismanager.window.setWindowIcon(QtGui.QIcon((os.path.join('res','shepherd.png'))))
0
votes

I found that under OS X with PyQT5, doing plt.get_current_fig_manager().window.setWindowIcon() has no effect. To get the dock icon to change you have to call setWindowIcon() on the QApplication instance, not on the window.

What worked for me is:

QApplication.instance().setWindowIcon(QtGui.QIcon(icon_path))

Do mind that QApplication.instance() will be None until you have actually created a figure, so do that first.