4

I want to add an icon to a custom button in a matplotlib figure toolbar. How can I do that? So far, I have the following code:

import matplotlib
matplotlib.rcParams["toolbar"] = "toolmanager"
import matplotlib.pyplot as plt
from matplotlib.backend_tools import ToolToggleBase

class NewTool(ToolToggleBase):
    ...[tool code]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], label="legend")
ax.legend()
fig.canvas.manager.toolmanager.add_tool("newtool", NewTool)
fig.canvas.manager.toolbar.add_tool(toolmanager.get_tool("newtool"), "toolgroup")
fig.show()

For now, the only thing that it does is adding a new button (which do what I want) but the icon is only the tool's name i.e.: "newtool". How can I change this for a custom icon like a png image?

fgoudra
  • 751
  • 8
  • 23

2 Answers2

8

The tool can have an attribute image, which denotes the path to a png image.

import matplotlib
matplotlib.rcParams["toolbar"] = "toolmanager"
import matplotlib.pyplot as plt
from matplotlib.backend_tools import ToolBase

class NewTool(ToolBase):
    image = r"C:\path\to\hiker.png"

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], label="legend")
ax.legend()
tm = fig.canvas.manager.toolmanager
tm.add_tool("newtool", NewTool)
fig.canvas.manager.toolbar.add_tool(tm.get_tool("newtool"), "toolgroup")
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 2
    I tried this solution, there is also a similar solution on matplotlib docs, but I cannot reproduce it. I get the following error: tm.add_tool("newtool", NewTool) AttributeError: 'NoneType' object has no attribute 'add_tool' Seems weird that plt.figure() does not contain canvas. Any ideas? – bad_locality Jun 25 '20 at 12:12
  • 2
    I get the same _AttributeError_ in Jupyter but it works fine within a standard IDE (even though I use the same interpreter in both). It looks like it has to do with the choice of backend. – Theom Oct 09 '20 at 11:26
  • 1
    Since this was the first page I stumbled upon in my searches, and I also wanted to know about Matplotlib in Jupyter, here is a Gihub issue where it is discussed: [Make the toolbar customizable · Issue #270 · matplotlib/ipympl · GitHub](https://github.com/matplotlib/ipympl/issues/270) – sdbbs Sep 12 '21 at 17:34
  • @bad_locality I believe you get this error if you do not use the rc option `matplotlib.rcParams["toolbar"] = "toolmanager"` – fgoudra Feb 06 '23 at 19:14
0

I tried this solution, there is also a similar solution on matplotlib docs, but I >cannot reproduce it. I get the following error: tm.add_tool("newtool", NewTool) >AttributeError: 'NoneType' object has no attribute 'add_tool' Seems weird that >plt.figure() does not contain canvas. Any ideas? – bad_locality Jun 25, 2020 at 12:12

I do not know if it's help, but I had the same issues at first and then i realized that I had not activated the interactive matplotlib option (%matplotlib with Python - spyder). Therefore no toolbar are associated since it only create a static figure.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33332077) – Piezoid Dec 08 '22 at 17:04