{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Tool Manager\n\nThis example demonstrates how to\n\n* modify the Toolbar\n* create tools\n* add tools\n* remove tools\n\nusing `matplotlib.backend_managers.ToolManager`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom matplotlib.backend_tools import ToolBase, ToolToggleBase\n\nplt.rcParams['toolbar'] = 'toolmanager'\n\n\nclass ListTools(ToolBase):\n    \"\"\"List all the tools controlled by the `ToolManager`.\"\"\"\n    default_keymap = 'm'  # keyboard shortcut\n    description = 'List Tools'\n\n    def trigger(self, *args, **kwargs):\n        print('_' * 80)\n        fmt_tool = \"{:12} {:45} {}\".format\n        print(fmt_tool('Name (id)', 'Tool description', 'Keymap'))\n        print('-' * 80)\n        tools = self.toolmanager.tools\n        for name in sorted(tools):\n            if not tools[name].description:\n                continue\n            keys = ', '.join(sorted(self.toolmanager.get_tool_keymap(name)))\n            print(fmt_tool(name, tools[name].description, keys))\n        print('_' * 80)\n        fmt_active_toggle = \"{!s:12} {!s:45}\".format\n        print(\"Active Toggle tools\")\n        print(fmt_active_toggle(\"Group\", \"Active\"))\n        print('-' * 80)\n        for group, active in self.toolmanager.active_toggle.items():\n            print(fmt_active_toggle(group, active))\n\n\nclass GroupHideTool(ToolToggleBase):\n    \"\"\"Show lines with a given gid.\"\"\"\n    default_keymap = 'S'\n    description = 'Show by gid'\n    default_toggled = True\n\n    def __init__(self, *args, gid, **kwargs):\n        self.gid = gid\n        super().__init__(*args, **kwargs)\n\n    def enable(self, *args):\n        self.set_lines_visibility(True)\n\n    def disable(self, *args):\n        self.set_lines_visibility(False)\n\n    def set_lines_visibility(self, state):\n        for ax in self.figure.get_axes():\n            for line in ax.get_lines():\n                if line.get_gid() == self.gid:\n                    line.set_visible(state)\n        self.figure.canvas.draw()\n\n\nfig = plt.figure()\nplt.plot([1, 2, 3], gid='mygroup')\nplt.plot([2, 3, 4], gid='unknown')\nplt.plot([3, 2, 1], gid='mygroup')\n\n# Add the custom tools that we created\nfig.canvas.manager.toolmanager.add_tool('List', ListTools)\nfig.canvas.manager.toolmanager.add_tool('Show', GroupHideTool, gid='mygroup')\n\n# Add an existing tool to new group `foo`.\n# It can be added as many times as we want\nfig.canvas.manager.toolbar.add_tool('zoom', 'foo')\n\n# Remove the forward button\nfig.canvas.manager.toolmanager.remove_tool('forward')\n\n# To add a custom tool to the toolbar at specific location inside\n# the navigation group\nfig.canvas.manager.toolbar.add_tool('Show', 'navigation', 1)\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}