
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/user_interfaces/toolmanager_sgskip.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_user_interfaces_toolmanager_sgskip.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_user_interfaces_toolmanager_sgskip.py:


============
Tool Manager
============

This example demonstrates how to

* modify the Toolbar
* create tools
* add tools
* remove tools

using `matplotlib.backend_managers.ToolManager`.

.. GENERATED FROM PYTHON SOURCE LINES 15-93

.. code-block:: Python


    import matplotlib.pyplot as plt

    from matplotlib.backend_tools import ToolBase, ToolToggleBase

    plt.rcParams['toolbar'] = 'toolmanager'


    class ListTools(ToolBase):
        """List all the tools controlled by the `ToolManager`."""
        default_keymap = 'm'  # keyboard shortcut
        description = 'List Tools'

        def trigger(self, *args, **kwargs):
            print('_' * 80)
            fmt_tool = "{:12} {:45} {}".format
            print(fmt_tool('Name (id)', 'Tool description', 'Keymap'))
            print('-' * 80)
            tools = self.toolmanager.tools
            for name in sorted(tools):
                if not tools[name].description:
                    continue
                keys = ', '.join(sorted(self.toolmanager.get_tool_keymap(name)))
                print(fmt_tool(name, tools[name].description, keys))
            print('_' * 80)
            fmt_active_toggle = "{!s:12} {!s:45}".format
            print("Active Toggle tools")
            print(fmt_active_toggle("Group", "Active"))
            print('-' * 80)
            for group, active in self.toolmanager.active_toggle.items():
                print(fmt_active_toggle(group, active))


    class GroupHideTool(ToolToggleBase):
        """Show lines with a given gid."""
        default_keymap = 'S'
        description = 'Show by gid'
        default_toggled = True

        def __init__(self, *args, gid, **kwargs):
            self.gid = gid
            super().__init__(*args, **kwargs)

        def enable(self, *args):
            self.set_lines_visibility(True)

        def disable(self, *args):
            self.set_lines_visibility(False)

        def set_lines_visibility(self, state):
            for ax in self.figure.get_axes():
                for line in ax.get_lines():
                    if line.get_gid() == self.gid:
                        line.set_visible(state)
            self.figure.canvas.draw()


    fig = plt.figure()
    plt.plot([1, 2, 3], gid='mygroup')
    plt.plot([2, 3, 4], gid='unknown')
    plt.plot([3, 2, 1], gid='mygroup')

    # Add the custom tools that we created
    fig.canvas.manager.toolmanager.add_tool('List', ListTools)
    fig.canvas.manager.toolmanager.add_tool('Show', GroupHideTool, gid='mygroup')

    # Add an existing tool to new group `foo`.
    # It can be added as many times as we want
    fig.canvas.manager.toolbar.add_tool('zoom', 'foo')

    # Remove the forward button
    fig.canvas.manager.toolmanager.remove_tool('forward')

    # To add a custom tool to the toolbar at specific location inside
    # the navigation group
    fig.canvas.manager.toolbar.add_tool('Show', 'navigation', 1)

    plt.show()


.. _sphx_glr_download_gallery_user_interfaces_toolmanager_sgskip.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: toolmanager_sgskip.ipynb <toolmanager_sgskip.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: toolmanager_sgskip.py <toolmanager_sgskip.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: toolmanager_sgskip.zip <toolmanager_sgskip.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
