|
| 1 | +import matplotlib |
| 2 | +matplotlib.use('GTK3Cairo') |
| 3 | +matplotlib.rcParams['toolbar'] = 'navigation' |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from matplotlib.backend_tools import ToolBase |
| 6 | + |
| 7 | + |
| 8 | +#Create a simple tool to list all the tools |
| 9 | +class ListTools(ToolBase): |
| 10 | + #keyboard shortcut |
| 11 | + keymap = 'm' |
| 12 | + #Name used as id, must be unique between tools of the same navigation |
| 13 | + name = 'List' |
| 14 | + description = 'List Tools' |
| 15 | + #Where to put it in the toolbar, -1 = at the end, None = Not in toolbar |
| 16 | + position = -1 |
| 17 | + |
| 18 | + def activate(self, event): |
| 19 | + #The most important attributes are navigation and figure |
| 20 | + self.navigation.list_tools() |
| 21 | + |
| 22 | + |
| 23 | +#A simple example of copy canvas |
| 24 | +#ref: at https://github.com/matplotlib/matplotlib/issues/1987 |
| 25 | +class CopyTool(ToolBase): |
| 26 | + keymap = 'ctrl+c' |
| 27 | + name = 'Copy' |
| 28 | + description = 'Copy canvas' |
| 29 | + position = -1 |
| 30 | + |
| 31 | + def activate(self, event): |
| 32 | + from gi.repository import Gtk, Gdk, GdkPixbuf |
| 33 | + clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) |
| 34 | + window = self.figure.canvas.get_window() |
| 35 | + x, y, width, height = window.get_geometry() |
| 36 | + pb = Gdk.pixbuf_get_from_window(window, x, y, width, height) |
| 37 | + clipboard.set_image(pb) |
| 38 | + |
| 39 | + |
| 40 | +fig = plt.figure() |
| 41 | +plt.plot([1, 2, 3]) |
| 42 | + |
| 43 | +#If we are in the old toolbar, don't try to modify it |
| 44 | +if matplotlib.rcParams['toolbar'] in ('navigation', 'None'): |
| 45 | + ##Add the custom tools that we created |
| 46 | + fig.canvas.manager.navigation.add_tool(ListTools) |
| 47 | + fig.canvas.manager.navigation.add_tool(CopyTool) |
| 48 | + |
| 49 | + ##Just for fun, lets remove the back button |
| 50 | + fig.canvas.manager.navigation.remove_tool('Back') |
| 51 | + |
| 52 | +plt.show() |
0 commit comments