maupertius
maupertius

Reputation: 1638

Change icon in a Matplotlib figure window

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.

Upvotes: 7

Views: 9285

Answers (5)

rem
rem

Reputation: 1388

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.

Upvotes: 0

Ryan Chin Sang
Ryan Chin Sang

Reputation: 51

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))

Upvotes: 2

Sijie Chen
Sijie Chen

Reputation: 11

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'))))

Upvotes: 0

gavin
gavin

Reputation: 31

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)

Upvotes: 3

maupertius
maupertius

Reputation: 1638

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.

Upvotes: 7

Related Questions