简体   繁体   中英

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.

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.

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)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM