|
| 1 | +'''This example demonstrates how to: |
| 2 | +* Modify the Toolbar |
| 3 | +* Create tools |
| 4 | +* Add tools |
| 5 | +* Remove tools |
| 6 | +Using `matplotlib.backend_managers.ToolManager` |
| 7 | +''' |
| 8 | + |
| 9 | + |
| 10 | +from __future__ import print_function |
| 11 | +import matplotlib |
| 12 | +matplotlib.use('GTK3Cairo') |
| 13 | +matplotlib.rcParams['toolbar'] = 'toolmanager' |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +from matplotlib.backend_tools import ToolBase, ToolToggleBase |
| 16 | +from gi.repository import Gtk, Gdk |
| 17 | + |
| 18 | + |
| 19 | +class ListTools(ToolBase): |
| 20 | + '''List all the tools controlled by the `ToolManager`''' |
| 21 | + # keyboard shortcut |
| 22 | + default_keymap = 'm' |
| 23 | + description = 'List Tools' |
| 24 | + |
| 25 | + def trigger(self, *args, **kwargs): |
| 26 | + print('_' * 80) |
| 27 | + print("{0:12} {1:45} {2}".format('Name (id)', |
| 28 | + 'Tool description', |
| 29 | + 'Keymap')) |
| 30 | + print('-' * 80) |
| 31 | + tools = self.toolmanager.tools |
| 32 | + for name in sorted(tools.keys()): |
| 33 | + if not tools[name].description: |
| 34 | + continue |
| 35 | + keys = ', '.join(sorted(self.toolmanager.get_tool_keymap(name))) |
| 36 | + print("{0:12} {1:45} {2}".format(name, |
| 37 | + tools[name].description, |
| 38 | + keys)) |
| 39 | + print('_' * 80) |
| 40 | + print("Active Toggle tools") |
| 41 | + print("{0:12} {1:45}".format("Group", "Active")) |
| 42 | + print('-' * 80) |
| 43 | + for group, active in self.toolmanager.active_toggle.items(): |
| 44 | + print("{0:12} {1:45}".format(group, active)) |
| 45 | + |
| 46 | + |
| 47 | +class GroupHideTool(ToolToggleBase): |
| 48 | + '''Hide lines with a given gid''' |
| 49 | + default_keymap = 'G' |
| 50 | + description = 'Hide by gid' |
| 51 | + |
| 52 | + def __init__(self, *args, **kwargs): |
| 53 | + self.gid = kwargs.pop('gid') |
| 54 | + ToolToggleBase.__init__(self, *args, **kwargs) |
| 55 | + |
| 56 | + def enable(self, *args): |
| 57 | + self.set_lines_visibility(False) |
| 58 | + |
| 59 | + def disable(self, *args): |
| 60 | + self.set_lines_visibility(True) |
| 61 | + |
| 62 | + def set_lines_visibility(self, state): |
| 63 | + gr_lines = [] |
| 64 | + for ax in self.figure.get_axes(): |
| 65 | + for line in ax.get_lines(): |
| 66 | + if line.get_gid() == self.gid: |
| 67 | + line.set_visible(state) |
| 68 | + self.figure.canvas.draw() |
| 69 | + |
| 70 | + |
| 71 | +fig = plt.figure() |
| 72 | +plt.plot([1, 2, 3], gid='mygroup') |
| 73 | +plt.plot([2, 3, 4], gid='unknown') |
| 74 | +plt.plot([3, 2, 1], gid='mygroup') |
| 75 | + |
| 76 | +# Add the custom tools that we created |
| 77 | +fig.canvas.manager.toolmanager.add_tool('List', ListTools) |
| 78 | +fig.canvas.manager.toolmanager.add_tool('Hide', GroupHideTool, gid='mygroup') |
| 79 | + |
| 80 | + |
| 81 | +# Add an existing tool to new group `foo`. |
| 82 | +# It can be added as many times as we want |
| 83 | +fig.canvas.manager.toolbar.add_tool('zoom', 'foo') |
| 84 | + |
| 85 | +# Remove the forward button |
| 86 | +fig.canvas.manager.toolmanager.remove_tool('forward') |
| 87 | + |
| 88 | +# To add a custom tool to the toolbar at specific location inside |
| 89 | +# the navigation group |
| 90 | +fig.canvas.manager.toolbar.add_tool('Hide', 'navigation', 1) |
| 91 | + |
| 92 | +plt.show() |
0 commit comments