Closed
Description
Bug summary
Prior to matplotlib 3.6
, I was able to use an icon for a custom tool in the toolbar of a figure which had a LA
mode (see attached). Now I can't seem to be able to load it anymore as a custom icon.
Code for reproduction
import os
import matplotlib
matplotlib.rcParams["toolbar"] = "toolmanager"
import matplotlib.pyplot as plt
from matplotlib.backend_tools import ToolToggleBase
class CustomTool(ToolToggleBase):
image = os.path.join(os.path.dirname(__file__), "icon.png")
if __name__ == "__main__":
fig = plt.figure()
toolmanager = fig.canvas.manager.toolmanager
toolbar = fig.canvas.manager.toolbar
toolmanager.add_tool("custom", CustomTool)
toolbar.add_tool("custom", "group")
Actual outcome
/home/[...]/mwe.py:4: UserWarning: Treat the new Tool classes introduced in v1.5 as experimental for now; the API and rcParam may change in future versions.
matplotlib.rcParams["toolbar"] = "toolmanager"
Traceback (most recent call last):
File "/home/[...]/mwe.py", line 19, in <module>
toolbar.add_tool("custom", "group")
File "/home/[...]/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 3377, in add_tool
self.add_toolitem(tool.name, group, position,
File "/home/[...]/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 957, in add_toolitem
button = NavigationToolbar2Tk._Button(frame, name, image_file, toggle,
File "/home/[...]/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 792, in _Button
NavigationToolbar2Tk._set_image_for_button(self, b)
File "/home/[...]/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 734, in _set_image_for_button
im_alt = _recolor_icon(im, foreground)
File "/home/[...]/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 722, in _recolor_icon
image_data[black_mask, :3] = color
ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (0,2)
Expected outcome
No errors should be raised and the icon should be displayed in the toolbar.
Additional information
Downgrading to matplotlib v 3.5.3
solves the issue. Don't know why this happens, but icons that have a RGBA
mode (like the default icons) don't seem to have this problem. The attached icon has a LA
mode.
Operating system
Ubuntu 22.04
Matplotlib Version
3.6.3
Matplotlib Backend
TkAgg
Python version
3.10.6
Jupyter version
N/A
Installation
pip