Skip to content

[Bug]: png icon image fails to load for manually defined tool buttons #22131

@bobeldering

Description

@bobeldering

Bug summary

The ToolBase class has the interface of overriding the property 'image' containing the path to an image file to be displayed as the icon of the tool button:
https://matplotlib.org/stable/api/backend_tools_api.html#matplotlib.backend_tools.ToolBase.image
This image fails to load on Qt5 backends for PNG image files.

Code for reproduction

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

class ExampleToolToggle(ToolToggleBase):
    image = "/tmp/example.png"

figure, axis = plt.subplots()
tool_manager = figure.canvas.manager.toolmanager
tool_manager.add_tool("example", ExampleToolToggle)
toolbar = figure.canvas.manager.toolbar
toolbar.add_tool(tool_manager.get_tool("example"), "zoompan", 0)
plt.show()

Actual outcome

The name of the tool is displayed instead of the image in the toolbar.

Expected outcome

The image displayed in the toolbar.

Additional information

This bug was introduced in version 3.3.0 (3.2.2 works fine).
The problem is caused by ToolbarQt calling the _icon method of NavigationToolbar2QT at:

button.setIcon(NavigationToolbar2QT._icon(self, image_file))

The _icon method unconditionally changes the path by inserting '_large' before the '.png' suffix:

name = name.replace('.png', '_large.png')

Before version 3.3.0 a _icon method of ToolbarQt itself was called, which didn't have the path mangling.

Operating system

Ubuntu

Matplotlib Version

3.5.1

Matplotlib Backend

Qt5Agg

Python version

Python 3.8.12

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions